-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_03.ex
107 lines (90 loc) · 2.62 KB
/
day_03.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
defmodule AdventOfCode.Y2019.Day03 do
@moduledoc """
--- Day 3: Crossed Wires ---
Problem Link: https://adventofcode.com/2019/day/3
Difficulty: xs
Tags: grid set not-fast-enough
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
import AdventOfCode.Algorithms.Geometry, only: [manhattan_distance: 2]
@origin {0, 0}
def input, do: InputReader.read_from_file(2019, 3)
def run(input \\ input()) do
input = parse(input)
{run_1(input), run_2(input)}
end
def run_1(input) do
input
|> Enum.map(&move(&1, @origin, []))
|> nearest_intersection()
end
def run_2(input) do
input
|> Enum.map(fn wire ->
wire
|> move(@origin, [])
|> Enum.dedup()
|> calculate_steps(0, %{})
end)
|> then(fn [a, b] ->
Map.merge(a, b, fn
_, 0, 0 -> :discard
_, a, b -> [a, b]
end)
end)
|> Map.filter(fn {_, v} -> is_list(v) end)
|> Enum.map(fn {_, steps} -> steps end)
|> Enum.min_by(fn [first, second] -> first + second end)
|> Enum.sum()
end
def parse(data) do
data
|> Transformers.lines()
|> Enum.map(fn line ->
line
|> String.split(",")
|> Enum.map(&parse_instruction/1)
end)
end
defp parse_instruction(instruction) do
instruction
|> String.split_at(1)
|> then(fn {dir, val} -> {dir, String.to_integer(val)} end)
end
defp move([instruction | []], current, results) do
Enum.concat(
results,
points_between(current, compute_next(current, instruction))
)
end
defp move([instruction | rest], current, results) do
next = compute_next(current, instruction)
move(
rest,
next,
Enum.concat(results, points_between(current, next))
)
end
defp compute_next({x, y}, {"L", value}), do: {x - value, y}
defp compute_next({x, y}, {"R", value}), do: {x + value, y}
defp compute_next({x, y}, {"U", value}), do: {x, y + value}
defp compute_next({x, y}, {"D", value}), do: {x, y - value}
defp points_between({x1, y1}, {x2, y2}) do
for x <- x1..x2, y <- y1..y2, do: {x, y}
end
defp nearest_intersection([first, second]) do
first
|> MapSet.new()
|> MapSet.intersection(MapSet.new(second))
|> MapSet.delete(@origin)
|> Enum.min_by(&manhattan_distance({0, 0}, &1))
|> manhattan_distance({0, 0})
end
defp calculate_steps([p], step, results), do: Map.put_new(results, p, step)
defp calculate_steps([first | rest], 0, %{}) do
calculate_steps(rest, 1, %{first => 0})
end
defp calculate_steps([first | rest], step, results) do
calculate_steps(rest, step + 1, Map.put_new(results, first, step))
end
end