-
Notifications
You must be signed in to change notification settings - Fork 3
/
elixir_day03.ex
111 lines (98 loc) · 3.02 KB
/
elixir_day03.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
defmodule ElixirDay03 do
@moduledoc """
Documentation for ElixirDay03.
"""
@doc """
Turns a filename into a list of "wires". A wire is a map.
See parse_line below for more info about wires.
"""
def parse(filename) do
File.read!(filename)
|> String.trim()
|> String.split("\n")
|> Enum.map(&parse_line/1)
end
@doc """
Turns a string into a "wire". A wire is a map with keys like {x, y}
representing grid coordinates. The value is the number of steps taken to
reach that coordinate.
"""
def parse_line(string) do
# When building a wire, we use a tuple of {visited, location, steps}.
# Visited is a map: {x, y} -> steps needed to get there
# Location is {x, y}, our current location.
# Steps is an int, current number of steps taken.
{visited, _location, _steps} =
string
|> String.trim()
|> String.split(",")
|> Enum.map(&parse_step/1)
|> Enum.reduce({%{}, {0, 0}, 0}, fn %{direction: direction, distance: distance}, acc ->
1..distance
|> Enum.reduce(acc, fn _, {visited, location, steps} ->
new_steps = steps + 1
new_location = move(location, direction)
# Possible bug: Self intersection overwrites "steps taken" with the later amount of steps
new_visited = Map.put(visited, new_location, new_steps)
{new_visited, new_location, new_steps}
end)
end)
visited
end
# parse_step("U58") -> %{direction: "U", distance: 58}
def parse_step(step) do
direction = String.slice(step, 0, 1)
distance = String.slice(step, 1, String.length(step) - 1) |> String.to_integer()
%{direction: direction, distance: distance}
end
def move({x, y}, "R"), do: {x + 1, y}
def move({x, y}, "L"), do: {x - 1, y}
def move({x, y}, "U"), do: {x, y + 1}
def move({x, y}, "D"), do: {x, y - 1}
def move({_x, _y}, _), do: raise("move: unknown direction")
def intersections(wires) do
wires
|> Enum.map(&Map.keys/1)
|> Enum.map(&MapSet.new/1)
|> Enum.reduce(&MapSet.intersection/2)
end
@doc """
Find all intersecting wires, convert the intersecting points to manhattan distance,
find the smallest manhattan distance.
"""
def part1(wires) do
wires
|> intersections()
|> Enum.map(fn {x, y} -> abs(x) + abs(y) end)
|> Enum.min()
end
@doc """
Find all intersecting wires, convert the intersecting points to cumulative steps
taken by all wires, find the smallest cumulative steps.
"""
def part2(wires) do
wires
|> intersections()
|> Enum.map(fn {x, y} -> find_sum_steps(wires, {x, y}) end)
|> Enum.min()
end
@doc """
Given a list of wires and a coordinate, find the sum of steps taken
to reach that coordinate.
"""
def find_sum_steps(wires, {x, y}) do
wires
|> Enum.reduce(0, fn wire, acc ->
acc + wire[{x, y}]
end)
end
def main() do
wires = parse("../input.txt")
wires
|> part1
|> IO.inspect(label: "Part 1")
wires
|> part2
|> IO.inspect(label: "Part 2")
end
end