-
Notifications
You must be signed in to change notification settings - Fork 2
/
aoc202217.ex
284 lines (227 loc) · 8.13 KB
/
aoc202217.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
defmodule AOC2022.Day17 do
@moduledoc """
Advent of Code 2022, day 17: Pyroclastic Flow.
"""
require AOC
@doc """
Parse input.
"""
def parse(puzzle_input) do
puzzle_input
|> String.split("", trim: true)
|> Enum.map(fn
">" -> :right
"<" -> :left
end)
|> create_infinite()
end
@doc """
Solve part 1.
"""
def part1(jets), do: find_tower_height(jets, 2022)
@doc """
Solve part 2.
"""
def part2(jets), do: find_tower_height(jets, 1_000_000_000_000)
@doc """
Find the tower height after dropping a given number of rocks.
## Example:
iex> jets = create_infinite([:left]) # Stack everything toward the left wall
iex> find_tower_height(jets, 1)
1
iex> find_tower_height(jets, 2)
4
iex> find_tower_height(jets, 5)
11
iex> find_tower_height(jets, 5555)
12221 = 11 * 1111
iex> find_tower_height(jets, 5557)
12225
"""
def find_tower_height(jets, number_of_rocks) do
floor = 0..6 |> Enum.into(%MapSet{}, fn x -> {x, 0} end)
# Warm up, drop a few rocks to blur out the effect of the floor
warm_up = min(200, number_of_rocks)
{jets, tower, warm_up_height} =
1..warm_up |> Enum.reduce({jets, floor, 0}, &simulate_rock_fall/2)
if warm_up == number_of_rocks do
warm_up_height
else
# Drop rocks until pattern repeats
initial = {jets, tower, warm_up_height, 0, %{}}
{jets, tower, height, period, _} =
(warm_up + 1)..number_of_rocks
|> Enum.reduce_while(initial, fn kind, {jets, tower, height, _, seen} ->
{new_jets, new_tower, new_height} = simulate_rock_fall(kind, {jets, tower, height})
position = {rem(kind, 5), peek_index(jets)}
if Map.has_key?(seen, position),
do: {:halt, {jets, tower, height, kind - seen[position], seen}},
else: {:cont, {new_jets, new_tower, new_height, 1, Map.put(seen, position, kind)}}
end)
num_periods = div(number_of_rocks - warm_up, period)
period_height = height - warm_up_height
# Drop the left-over rocks at the top of the tower
{_, _, height} =
(warm_up + num_periods * period + 1)..number_of_rocks//1
|> Enum.reduce({jets, tower, height}, &simulate_rock_fall/2)
# Add in the non-dropped periodic chunks
height + (num_periods - 1) * period_height
end
end
@doc """
Simulate one rock falling.
## Example:
iex> simulate_rock_fall(5, {create_infinite([:right]), MapSet.new([{6, 0}]), 0})
{{[:right], 0, 1}, MapSet.new([{5, 1}, {5, 2}, {6, 0}, {6, 1}, {6, 2}]), 2}
"""
def simulate_rock_fall(kind, {jets, tower, height}) do
{jets, rock} =
Stream.cycle([nil])
|> Enum.reduce_while({jets, start_rock(kind, height + 4)}, fn _, {jets, rock} ->
{jet, jets} = get_next(jets)
blown_rock = rock |> blow_rock(jet, tower)
dropped_rock = blown_rock |> drop_rock()
if not_touch(tower, dropped_rock),
do: {:cont, {jets, dropped_rock}},
else: {:halt, {jets, blown_rock}}
end)
{jets, add_rock(tower, rock), tower_height(height, rock)}
end
@doc """
Check if a rock overlaps any part of the tower.
## Examples:
iex> tower = MapSet.new([{0, 0}, {1, 1}, {2, 0}])
iex> not_touch(tower, [{2, 1}, {2, 2}, {3, 1}, {3, 2}])
true
iex> not_touch(tower, [{1, 1}, {1, 2}, {2, 1}, {2, 2}])
false
"""
def not_touch(tower, rock),
do: rock |> Enum.all?(fn stone -> not MapSet.member?(tower, stone) end)
@doc """
Add a rock to the tower.
## Example:
iex> add_rock(MapSet.new([{0, 0}, {0, 2}]), [{0, 1}, {0, 2}, {1 , 1}, {1, 2}])
MapSet.new([{0, 0}, {0, 2}, {0, 1}, {0, 2}, {1 , 1}, {1, 2}])
"""
def add_rock(tower, rock), do: MapSet.union(tower, Enum.into(rock, %MapSet{}))
@doc """
Update the tower height when placing a new rock.
## Examples:
iex> tower_height(1, MapSet.new([{4, 1}, {4, 2}, {5, 1}, {5, 2}]))
2
iex> tower_height(4, MapSet.new([{4, 1}, {4, 2}, {5, 1}, {5, 2}]))
4
"""
def tower_height(height, rock),
do: max(height, rock |> Enum.map(fn {_, y} -> y end) |> Enum.max())
@doc """
Wrap an enumerable to simulate an infinite stream of cycled elements.
We could use Stream.cycle() for this, but streams are best for one-pass
operations. Here, we want to continously take 1 element.
## Example:
iex> create_infinite(1..3)
{1..3, 0, 3}
"""
def create_infinite(stream), do: {stream, 0, stream |> Enum.to_list() |> length}
@doc """
Get next element in an infinite stream.
## Example:
iex> stream = create_infinite([4, 2])
iex> {next, stream} = get_next(stream)
iex> next
4
iex> {next, stream} = get_next(stream)
iex> next
2
iex> {next, _} = get_next(stream)
iex> next
4
"""
def get_next({stream, idx, len}), do: {Enum.at(stream, idx), {stream, rem(idx + 1, len), len}}
@doc """
Get number of elements in one cycle of the infinite stream.
## Example:
iex> stream = create_infinite([4, 1, 8, 0, 6])
iex> num_elements(stream)
5
"""
def num_elements({_, _, len}), do: len
@doc """
Peek at the index of the next element.
## Example:
iex> stream = create_infinite([4, 1, 8, 0, 6])
iex> {_, stream} = get_next(stream)
iex> {_, stream} = get_next(stream)
iex> peek_index(stream)
2
iex> {_, stream} = get_next(stream)
iex> {_, stream} = get_next(stream)
iex> {_, stream} = get_next(stream)
iex> {_, stream} = get_next(stream)
iex> peek_index(stream)
1
"""
def peek_index({_, idx, _}), do: idx
@doc """
Coordinates to one new rock as it starts falling.
Each rock appears so that its left edge is two units away from the left wall
and its bottom edge is three units above the highest rock in the room (or the
floor, if there isn't one).
The rocks cycle through the following five patterns.
#
.#. ..# #
### ..# # ##
#### .#. ### # ##
## Examples:
iex> start_rock(1, 0)
[{2, 0}, {3, 0}, {4, 0}, {5, 0}]
iex> start_rock(4, 3)
[{2, 3}, {2, 4}, {2, 5}, {2, 6}]
iex> start_rock(5, 9)
[{2, 9}, {2, 10}, {3, 9}, {3, 10}]
iex> start_rock(2, 7) === start_rock(132, 7)
true
iex> start_rock(2, 7) === start_rock(123, 7)
false
iex> start_rock(2, 7) === start_rock(432, 8)
false
"""
def start_rock(1, y), do: [{2, y}, {3, y}, {4, y}, {5, y}]
def start_rock(2, y), do: [{2, y + 1}, {3, y}, {3, y + 1}, {3, y + 2}, {4, y + 1}]
def start_rock(3, y), do: [{2, y}, {3, y}, {4, y}, {4, y + 1}, {4, y + 2}]
def start_rock(4, y), do: [{2, y}, {2, y + 1}, {2, y + 2}, {2, y + 3}]
def start_rock(5, y), do: [{2, y}, {2, y + 1}, {3, y}, {3, y + 1}]
def start_rock(number, y), do: start_rock(rem(number - 1, 5) + 1, y)
@doc """
Blow the rock one unit to the left or right.
The tower walls are to the left of 0 and to the right of 6, with the tower being 7 unit wide.
## Examples:
iex> blow_rock([{2, 0}, {2, 1}, {3, 0}, {3, 1}], :left, %MapSet{})
[{1, 0}, {1, 1}, {2, 0}, {2, 1}]
iex> blow_rock([{3, 2}, {4, 2}, {5, 2}, {6, 2}], :right, %MapSet{})
[{3, 2}, {4, 2}, {5, 2}, {6, 2}]
"""
def blow_rock(rock, :right, tower) do
if Enum.any?(rock, fn {x, _} -> x >= 6 end),
do: rock,
else: Enum.map(rock, fn {x, y} -> {x + 1, y} end) |> avoid_collision(tower, rock)
end
def blow_rock(rock, :left, tower) do
if Enum.any?(rock, fn {x, _} -> x <= 0 end),
do: rock,
else: Enum.map(rock, fn {x, y} -> {x - 1, y} end) |> avoid_collision(tower, rock)
end
defp avoid_collision(rock, tower, original_rock),
do: if(MapSet.disjoint?(tower, rock |> Enum.into(%MapSet{})), do: rock, else: original_rock)
@doc """
Drop the rock one unit down.
## Example:
iex> drop_rock([{4, 3}, {4, 4}, {5, 3}, {5, 4}])
[{4, 2}, {4, 3}, {5, 2}, {5, 3}]
"""
def drop_rock(rock), do: Enum.map(rock, fn {x, y} -> {x, y - 1} end)
def main(args) do
Enum.map(args, fn path -> AOC.solve(path, &parse/1, &part1/1, &part2/1) end)
end
end