AoC 2021 - Day 9: Smoke Basin
Setup
Mix.install([
{:kino, "~> 0.4.1"},
{:vega_lite, "~> 0.1.2"}
])
alias VegaLite, as: Vl
input = Kino.Input.textarea("Please paste your input:")
Part 1
These caves seem to be lava tubes. Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain.
If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer. The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input).
Smoke flows to the lowest point of the area it’s in. For example, consider the following heightmap:
2199943210
3987894921
9856789892
8767896789
9899965678
Each number corresponds to the height of a particular location, where 9
is the highest and 0
is the lowest a location can be.
Your first goal is to find the low points - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.)
In the above example, there are four low points, all highlighted: two are in the first row (a 1
and a 0
), one is in the third row (a 5
), and one is in the bottom row (also a 5
). All other locations on the heightmap have some lower adjacent location, and so are not low points.
The risk level of a low point is 1 plus its height. In the above example, the risk levels of the low points are 2
, 1
, 6
, and 6
. The sum of the risk levels of all low points in the heightmap is therefore 15
.
Find all of the low points on your heightmap. What is the sum of the risk levels of all low points on your heightmap?
lines = Kino.Input.read(input)
grid =
for {line, y} <- Enum.with_index(String.split(lines, "\n", trim: true)),
{height, x} <- Enum.with_index(String.to_charlist(line)),
into: %{} do
{{x, y}, height - ?0}
end
low_points =
grid
|> Enum.filter(fn {{x, y}, height} ->
top = grid[{x, y - 1}]
right = grid[{x + 1, y}]
bottom = grid[{x, y + 1}]
left = grid[{x - 1, y}]
height < top and height < right and height < bottom and height < left
end)
low_points
|> Enum.map(fn {_, height} -> height + 1 end)
|> Enum.sum()
Part 2
Next, you need to find the largest basins so you know what areas are most important to avoid.
A basin is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height 9
do not count as being in any basin, and all other locations will always be part of exactly one basin.
The size of a basin is the number of locations within the basin, including the low point. The example above has four basins.
The top-left basin, size 3
:
2199943210
3987894921
9856789892
8767896789
9899965678
The top-right basin, size 9
:
2199943210
3987894921
9856789892
8767896789
9899965678
The middle basin, size 14
:
2199943210
3987894921
9856789892
8767896789
9899965678
The bottom-right basin, size 9
:
2199943210
3987894921
9856789892
8767896789
9899965678
Find the three largest basins and multiply their sizes together. In the above example, this is 9 * 14 * 9 = 1134
.
What do you get if you multiply together the sizes of the three largest basins?
defmodule Basin do
def plot(point, grid) do
visit(MapSet.new(), point, grid)
end
defp visit(set, {x, y} = point, grid) do
if grid[point] in [9, nil] or point in set do
set
else
set
|> MapSet.put(point)
|> visit({x + 1, y}, grid)
|> visit({x - 1, y}, grid)
|> visit({x, y + 1}, grid)
|> visit({x, y - 1}, grid)
end
end
end
low_points
|> Enum.map(fn {point, _} ->
point
|> Basin.plot(grid)
|> MapSet.size()
end)
|> Enum.sort(:desc)
|> Enum.take(3)
|> Enum.product()
Visualization
low_points_set = for {point, _} <- low_points, into: MapSet.new(), do: point
Vl.new(width: 700, height: 700)
|> Vl.data_from_values(
Enum.map(grid, fn {{x, y}, height} ->
%{"x" => x, "y" => y, "height" => height, "lowpoint" => {x, y} in low_points_set}
end)
)
|> Vl.encode_field(:x, "x", type: :quantitative, axis: false)
|> Vl.encode_field(:y, "y", type: :quantitative, axis: false)
|> Vl.layers([
Vl.new()
|> Vl.mark(:circle, size: 60, opacity: 0.8)
|> Vl.encode_field(:color, "height", type: :quantitative, scale: [range: ["#2d3080", "#1fe8ff"]])
|> Vl.encode_field(:tooltip, "height"),
Vl.new()
|> Vl.mark(:text, text: "☠️", size: 10)
|> Vl.transform(filter: "datum.lowpoint == true")
])