-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc202111.ex
182 lines (151 loc) · 4.71 KB
/
aoc202111.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
defmodule AOC2021.Day11 do
@moduledoc """
Advent of Code 2021, day 11: Dumbo Octopus
"""
require AOC
@doc """
Parse input
"""
def parse(puzzle_input) do
puzzle_input
|> String.split("\n")
|> Enum.with_index()
|> Enum.map(fn {line, row} ->
line
|> String.split("", trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.with_index()
|> Enum.map(fn {level, col} -> {{row, col}, level} end)
end)
|> Enum.concat()
|> Enum.into(%{})
end
@doc """
Solve part 1
"""
def part1(input) do
{_, num_flashes} =
1..100
|> Enum.reduce({input, 0}, fn _, {energies, done_flashes} ->
{new_energies, new_flashes} = energies |> step()
{new_energies, done_flashes + new_flashes}
end)
num_flashes
end
@doc """
Solve part 2
"""
def part2(input) do
input |> synchronize()
end
@doc """
Do one step of energy increase
## Examples:
35 --> 57
79 90
iex> step(%{{0, 0} => 3, {0, 1} => 5, {1, 0} => 7, {1, 1} => 9})
{%{{0, 0} => 5, {0, 1} => 7, {1, 0} => 9, {1, 1} => 0}, 1}
11111 34543
19991 40004
19191 --> 50005
19991 40004
11111 34543
iex> step(%{
...> {0, 0} => 1, {0, 1} => 1, {0, 2} => 1, {0, 3} => 1, {0, 4} => 1,
...> {1, 0} => 1, {1, 1} => 9, {1, 2} => 9, {1, 3} => 9, {1, 4} => 1,
...> {2, 0} => 1, {2, 1} => 9, {2, 2} => 1, {2, 3} => 9, {2, 4} => 1,
...> {3, 0} => 1, {3, 1} => 9, {3, 2} => 9, {3, 3} => 9, {3, 4} => 1,
...> {4, 0} => 1, {4, 1} => 1, {4, 2} => 1, {4, 3} => 1, {4, 4} => 1})
{%{{0, 0} => 3, {0, 1} => 4, {0, 2} => 5, {0, 3} => 4, {0, 4} => 3,
{1, 0} => 4, {1, 1} => 0, {1, 2} => 0, {1, 3} => 0, {1, 4} => 4,
{2, 0} => 5, {2, 1} => 0, {2, 2} => 0, {2, 3} => 0, {2, 4} => 5,
{3, 0} => 4, {3, 1} => 0, {3, 2} => 0, {3, 3} => 0, {3, 4} => 4,
{4, 0} => 3, {4, 1} => 4, {4, 2} => 5, {4, 3} => 4, {4, 4} => 3}, 9}
"""
def step(energies), do: energies |> add_one(Map.keys(energies)) |> flash()
@doc """
Add 1 to the given positions, ignore invalid positions
## Example:
(3) (5) ( ) --> 4 6
(7) 9 8 9
iex> add_one(%{{0, 0} => 3, {0, 1} => 5, {1, 0} => 7, {1, 1} => 9}, [{0, 0}, {0, 1}, {0, 2}, {1, 0}])
%{{0, 0} => 4, {0, 1} => 6, {1, 0} => 8, {1, 1} => 9}
"""
def add_one(energies, positions) do
positions
|> Enum.reduce(energies, fn pos, acc -> acc |> Map.replace(pos, Map.get(acc, pos, 0) + 1) end)
end
@doc """
Perform flashes for the given energies
## Example:
0 7 --> 2 9
9 10 0 0
iex> flash(%{{0, 0} => 0, {0, 1} => 7, {1, 0} => 9, {1, 1} => 10})
{
%{{0, 0} => 2, {0, 1} => 9, {1, 0} => 0, {1, 1} => 0},
2
}
"""
def flash(energies), do: flash(energies, [])
def flash(energies, already_flashed) do
flash(
energies,
Map.keys(energies) |> Enum.filter(fn pos -> Map.get(energies, pos) > 9 end),
already_flashed
)
end
def flash(energies, [], already_flashed) do
flashed_energies =
Enum.reduce(already_flashed, energies, fn pos, acc ->
Map.update!(acc, pos, fn _ -> 0 end)
end)
if flashed_energies |> Map.values() |> Enum.all?(&(&1 <= 9)) do
{flashed_energies, already_flashed |> length()}
else
flash(
flashed_energies,
Map.keys(flashed_energies)
|> Enum.filter(fn pos -> Map.get(flashed_energies, pos) > 9 end),
already_flashed
)
end
end
def flash(energies, [next | to_flash], already_flashed) do
flash(energies |> add_one(next |> neighbors()), to_flash, [next | already_flashed])
end
@doc """
Find all possible neighbors of an octopus position, including invalid ones
## Example:
iex> neighbors({0, 3})
[{-1, 2}, {0, 2}, {1, 2}, {-1, 3}, {1, 3}, {-1, 4}, {0, 4}, {1, 4}]
"""
def neighbors({x, y}) do
[
{x - 1, y - 1},
{x, y - 1},
{x + 1, y - 1},
{x - 1, y},
{x + 1, y},
{x - 1, y + 1},
{x, y + 1},
{x + 1, y + 1}
]
end
@doc """
Count steps until all octopuses flash synchronously
## Example:
5 6 --> 6 7 --> 0 0
7 8 8 9 0 0
iex> synchronize(%{{0, 0} => 5, {0, 1} => 6, {1, 0} => 7, {1, 1} => 8})
2
"""
def synchronize(energies), do: synchronize(energies, 0)
def synchronize(energies, steps) do
if energies |> Map.values() |> Enum.all?(&(&1 == 0)),
do: steps,
else: synchronize(energies |> step() |> elem(0), steps + 1)
end
def main(args) do
Enum.map(args, fn path -> AOC.solve(path, &parse/1, &part1/1, &part2/1) end)
end
end