-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_09.ex
60 lines (49 loc) · 1.69 KB
/
day_09.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
defmodule Aoc.Y2021.Day09 do
@moduledoc """
Solved https://adventofcode.com/2021/day/9
"""
import Aoc.Helper.IO
def solve_part1(data), do: data |> get_risk_levels() |> Enum.sum()
def solve_part2(data) do
data
|> get_low_points()
|> Enum.map(fn {i, j} -> bfs(data, i, j, MapSet.new()) end)
|> Enum.map(&MapSet.size/1)
|> Enum.sort(:desc)
|> Enum.take(3)
|> Enum.product()
end
def get_risk_levels(data),
do: data |> get_low_points() |> Enum.map(fn {i, j} -> Enum.at(Enum.at(data, i), j) + 1 end)
def get_low_points(data),
do:
for(
i <- 0..(length(data) - 1),
j <- 0..(length(Enum.at(data, 0)) - 1),
lowest?(data, i, j),
do: {i, j}
)
def lowest?(data, i, j), do: Enum.at(Enum.at(data, i), j) < smallest_neighbor(data, i, j)
def smallest_neighbor(data, i, j),
do: Enum.min(Enum.map(get_neighbors(data, i, j), fn {x, y} -> Enum.at(Enum.at(data, x), y) end))
def get_neighbors(data, i, j) do
for [x, y] <- [[-1, 0], [0, 1], [1, 0], [0, -1]],
0 <= i + x and i + x < length(data) and 0 <= j + y and j + y < length(Enum.at(data, i)) do
{i + x, j + y}
end
end
def bfs(data, i, j, visited) do
visited = MapSet.put(visited, {i, j})
for {x, y} <- get_neighbors(data, i, j),
Enum.at(Enum.at(data, x), y) != 9 and {x, y} not in visited,
reduce: visited do
visited -> MapSet.union(MapSet.put(visited, {x, y}), bfs(data, x, y, visited))
end
end
def get_input() do
get_string_input("2021", "09")
|> String.split("\n")
|> Enum.map(&(&1 |> String.graphemes() |> Enum.map(fn v -> String.to_integer(v) end)))
end
def solved_status(), do: :solved
end