-
Notifications
You must be signed in to change notification settings - Fork 2
/
aoc202109.ex
152 lines (124 loc) · 3.63 KB
/
aoc202109.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
defmodule AOC2021.Day09 do
@moduledoc """
Advent of Code 2021, day 9: Smoke Basin
"""
require AOC
alias AOC2021.Day09.HeightMap
@doc """
Parse input
"""
def parse(puzzle_input) do
puzzle_input |> HeightMap.from_str()
end
@doc """
Solve part 1
"""
def part1(height_map) do
height_map
|> HeightMap.to_list()
|> Enum.filter(&HeightMap.low_point?(height_map, &1))
|> Enum.map(&(height_map.heights[&1] + 1))
|> Enum.sum()
end
@doc """
Solve part 2
"""
def part2(height_map) do
basin_sources =
height_map |> HeightMap.to_list() |> Enum.filter(&HeightMap.low_point?(height_map, &1))
basin_sources
|> Enum.map(fn source -> HeightMap.fill_basin(height_map, source) |> length() end)
|> Enum.sort(:desc)
|> Enum.take(3)
|> Enum.product()
end
def main(args) do
Enum.map(args, fn path -> AOC.solve(path, &parse/1, &part1/1, &part2/1) end)
end
end
defmodule AOC2021.Day09.HeightMap do
@moduledoc """
Struct representing a map with heights, useful as a means of creating positions set only once
"""
defstruct heights: nil, positions: nil
@doc """
Create a HeightMap from a string
## Example:
iex> from_str("09\\n55")
%HeightMap{
heights: %{{0, 0} => 0, {0, 1} => 9, {1, 0} => 5, {1, 1} => 5},
positions: MapSet.new([{0, 0}, {0, 1}, {1, 0}, {1, 1}])
}
"""
def from_str(text) do
heights =
text
|> String.split("\n")
|> Enum.with_index()
|> Enum.reduce(%{}, fn {line, row}, acc ->
line
|> String.split("", trim: true)
|> Enum.with_index()
|> Enum.reduce(acc, fn {height, col}, acc ->
Map.put(acc, {row, col}, height |> String.to_integer())
end)
end)
struct(__MODULE__, %{:heights => heights, :positions => heights |> Map.keys() |> MapSet.new()})
end
@doc """
Represent a HeightMap as a list of its positions
## Example:
iex> from_str("09\\n55") |> to_list()
[{0, 0}, {0, 1}, {1, 0}, {1, 1}]
"""
def to_list(height_map), do: height_map.heights |> Map.keys()
@doc """
Find neighbors of a position
## Examples:
iex> neighbors(from_str("123\\n456\\n789"), {1, 1})
[{0, 1}, {1, 0}, {2, 1}, {1, 2}]
iex> neighbors(from_str("123\\n456\\n789"), {0, 0})
[{1, 0}, {0, 1}]
"""
def neighbors(%{positions: positions}, {x, y}) do
Enum.filter([{x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}], &MapSet.member?(positions, &1))
end
@doc """
Check if a position is lower than all its neighbors
## Examples:
4 5 6
7 4 5
iex> low_point?(from_str("456\\n745"), {0, 1})
false
iex> low_point?(from_str("456\\n745"), {1, 1})
true
"""
def low_point?(height_map, pos) do
neighbors(height_map, pos)
|> Enum.map(fn neighbor -> height_map.heights[pos] < height_map.heights[neighbor] end)
|> Enum.all?()
end
@doc """
Find the basin connected to source
## Examples:
7 6 9 . . .
9 9 6 -> . . #
(5)3 2 # # #
iex> fill_basin(from_str("769\\n996\\n532"), {2, 0})
[{1, 2}, {2, 0}, {2, 1}, {2, 2}]
"""
def fill_basin(height_map, source),
do: fill_basin(height_map, [source], MapSet.new())
def fill_basin(_height_map, [], filled), do: filled |> MapSet.to_list() |> Enum.sort()
def fill_basin(height_map, [current | queue], filled) do
if height_map.heights[current] == 9 or MapSet.member?(filled, current) do
fill_basin(height_map, queue, filled)
else
fill_basin(
height_map,
queue ++ neighbors(height_map, current),
MapSet.put(filled, current)
)
end
end
end