-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_19.ex
203 lines (189 loc) · 4.47 KB
/
day_19.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
defmodule Aoc.Y2021.Day19 do
@moduledoc """
Solved https://adventofcode.com/2021/day/19
"""
import Aoc.Helper.IO
def solve_part1(scanners), do: scanners |> count()
def solve_part2(scanners), do: scanners |> find_largest_distance()
defp count({[], beacons}), do: beacons |> MapSet.size()
defp count({scanners, beacons}) do
Enum.reduce(scanners, {scanners, beacons}, fn scanner, {rest, beacons} ->
if (matched = find_match_point(scanner, beacons)) != [] do
{List.delete(rest, scanner), update_beacons(matched, beacons, scanner)}
else
{rest, beacons}
end
end)
|> count()
end
defp count(scanners), do: count({scanners, scanners |> hd |> MapSet.new()})
defp find_largest_distance({[], _beacons, points}),
do:
Enum.flat_map(points, fn point1 -> Enum.map(points, fn point2 -> manhatten_distance(point1, point2) end) end)
|> Enum.max()
defp find_largest_distance({scanners, beacons, points}) do
Enum.reduce(scanners, {scanners, beacons, points}, fn scanner, {rest, beacons, points} ->
if (matched = find_match_point(scanner, beacons)) != [] do
{List.delete(rest, scanner), update_beacons(matched, beacons, scanner), [matched |> hd() |> elem(0) | points]}
else
{rest, beacons, points}
end
end)
|> find_largest_distance()
end
defp find_largest_distance(scanners), do: find_largest_distance({scanners, scanners |> hd |> MapSet.new(), []})
defp update_beacons([{point, rmatrix}], beacons, scanner),
do: Enum.map(scanner, &add(rotate(&1, rmatrix), point)) |> MapSet.new() |> MapSet.union(beacons)
defp find_match_point(scanner, beacons) do
Enum.flat_map(rotations(), fn rmatrix ->
scanner
|> Enum.map(fn point -> rotate(point, rmatrix) end)
|> Enum.flat_map(&Enum.map(beacons, fn point -> subtract(&1, point) end))
|> Enum.frequencies()
|> Enum.reject(&(&1 |> elem(1) < 12))
|> Enum.map(&{&1 |> elem(0), rmatrix})
end)
end
def get_input() do
get_string_input("2021", "19")
|> String.split("\n\n", trim: true)
|> Enum.map(
&(String.split(&1, "\n", trim: true)
|> Enum.slice(1..-1)
|> Enum.map(fn nums ->
String.split(nums, ",", trim: true)
|> Enum.map(fn num -> String.to_integer(num) end)
end))
)
end
defp add([x, y, z], [a, b, c]), do: [x + a, y + b, z + c]
defp subtract([x, y, z], [a, b, c]), do: [a - x, b - y, c - z]
defp manhatten_distance([a, b, c], [d, e, f]), do: abs(d - a) + abs(e - b) + abs(f - c)
defp rotate([x, y, z], [[a, b, c], [d, e, f], [g, h, i]]),
do: [x * a + y * b + z * c, x * d + y * e + z * f, x * g + y * h + z * i]
defp rotations() do
[
[
[-1, 0, 0],
[0, 0, -1],
[0, -1, 0]
],
[
[0, -1, 0],
[-1, 0, 0],
[0, 0, -1]
],
[
[0, 0, -1],
[0, -1, 0],
[-1, 0, 0]
],
[
[-1, 0, 0],
[0, -1, 0],
[0, 0, 1]
],
[
[0, -1, 0],
[0, 0, 1],
[-1, 0, 0]
],
[
[0, 0, 1],
[-1, 0, 0],
[0, -1, 0]
],
[
[-1, 0, 0],
[0, 1, 0],
[0, 0, -1]
],
[
[0, 1, 0],
[0, 0, -1],
[-1, 0, 0]
],
[
[0, 0, -1],
[-1, 0, 0],
[0, 1, 0]
],
[
[-1, 0, 0],
[0, 0, 1],
[0, 1, 0]
],
[
[0, 1, 0],
[-1, 0, 0],
[0, 0, 1]
],
[
[0, 0, 1],
[0, 1, 0],
[-1, 0, 0]
],
[
[1, 0, 0],
[0, -1, 0],
[0, 0, -1]
],
[
[0, -1, 0],
[0, 0, -1],
[1, 0, 0]
],
[
[0, 0, -1],
[1, 0, 0],
[0, -1, 0]
],
[
[1, 0, 0],
[0, 0, 1],
[0, -1, 0]
],
[
[0, -1, 0],
[1, 0, 0],
[0, 0, 1]
],
[
[0, 0, 1],
[0, -1, 0],
[1, 0, 0]
],
[
[1, 0, 0],
[0, 0, -1],
[0, 1, 0]
],
[
[0, 1, 0],
[1, 0, 0],
[0, 0, -1]
],
[
[0, 0, -1],
[0, 1, 0],
[1, 0, 0]
],
[
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
],
[
[0, 1, 0],
[0, 0, 1],
[1, 0, 0]
],
[
[0, 0, 1],
[1, 0, 0],
[0, 1, 0]
]
]
end
def solved_status(), do: :solved
end