this repo has no description
at master 1.1 kB view raw
1defmodule Wires do 2 @directions %{ 3 ?U => :up, 4 ?D => :down, 5 ?R => :right, 6 ?L => :left 7 } 8 9 @moves %{ 10 up: {0, 1}, 11 down: {0, -1}, 12 right: {1, 0}, 13 left: {-1, 0} 14 } 15 16 def load(list) do 17 list 18 |> Enum.map(&parse/1) 19 |> wire() 20 end 21 22 defp parse(<<c, rest::binary>>) do 23 {Map.fetch!(@directions, c), String.to_integer(rest)} 24 end 25 26 defp wire(steps) do 27 steps 28 |> Enum.reduce({[], {0, 0}}, &into_coordinates/2) 29 |> elem(0) 30 |> MapSet.new() 31 end 32 33 defp into_coordinates({_, 0}, {points, curr}), do: {[curr | points], curr} 34 defp into_coordinates({dir, n}, {points, {x, y}}) do 35 {dx, dy} = Map.fetch!(@moves, dir) 36 next = {x + dx, y + dy} 37 38 into_coordinates({dir, n - 1}, {[next | points], next}) 39 end 40end 41 42w1 = 43 IO.read(:line) 44 |> String.trim() 45 |> String.split(",") 46 |> Wires.load() 47w2 = 48 IO.read(:line) 49 |> String.trim() 50 |> String.split(",") 51 |> Wires.load() 52 53MapSet.intersection(w1, w2) 54|> MapSet.to_list() 55|> Enum.map(fn {x, y} -> abs(x) + abs(y) end) 56|> Enum.min() 57|> IO.inspect()