— Day 14: Regolith Reservoir —
Mix.install([
{:nx, "~> 0.4.1"}
])
:ok
Part 1
The distress signal leads you to a giant waterfall! Actually, hang on - the signal seems like it’s coming from the waterfall itself, and that doesn’t make any sense. However, you do notice a little path that leads behind the waterfall.
Correction: the distress signal leads you behind a giant waterfall! There seems to be a large cave system here, and the signal definitely leads further inside.
As you begin to make your way deeper underground, you feel the ground rumble for a moment. Sand begins pouring into the cave! If you don’t quickly figure out where the sand is going, you could quickly become trapped!
Fortunately, your familiarity with analyzing the path of falling material will come in handy here. You scan a two-dimensional vertical slice of the cave above you (your puzzle input) and discover that it is mostly air with structures made of rock.
Your scan traces the path of each solid rock structure and reports the x,y coordinates that form the shape of the path, where x represents distance to the right and y represents distance down. Each path appears as a single line of text in your scan. After the first point of each path, each point indicates the end of a straight horizontal or vertical line to be drawn from the previous point. For example:
example_input = """
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9
"""
"498,4 -> 498,6 -> 496,6\n503,4 -> 502,4 -> 502,9 -> 494,9\n"
This scan means that there are two paths of rock; the first path consists of two straight lines, and the second path consists of three straight lines. (Specifically, the first path consists of a line of rock from 498,4 through 498,6 and another line of rock from 498,6 through 496,6.)
The sand is pouring into the cave from point 500,0.
Drawing rock as #, air as ., and the source of the sand as +, this becomes:
4 5 5
9 0 0
4 0 3
0 ......+...
1 ..........
2 ..........
3 ..........
4 ....#...##
5 ....#...#.
6 ..###...#.
7 ........#.
8 ........#.
9 #########.
Sand is produced one unit at a time, and the next unit of sand is not produced until the previous unit of sand comes to rest. A unit of sand is large enough to fill one tile of air in your scan.
A unit of sand always falls down one step if possible. If the tile immediately below is blocked (by rock or sand), the unit of sand attempts to instead move diagonally one step down and to the left. If that tile is blocked, the unit of sand attempts to instead move diagonally one step down and to the right. Sand keeps moving as long as it is able to do so, at each step trying to move down, then down-left, then down-right. If all three possible destinations are blocked, the unit of sand comes to rest and no longer moves, at which point the next unit of sand is created back at the source.
So, drawing sand that has come to rest as o, the first unit of sand simply falls straight down and then stops:
......+...
..........
..........
..........
....#...##
....#...#.
..###...#.
........#.
......o.#.
#########.
The second unit of sand then falls straight down, lands on the first one, and then comes to rest to its left:
......+...
..........
..........
..........
....#...##
....#...#.
..###...#.
........#.
.....oo.#.
#########.
After a total of five units of sand have come to rest, they form this pattern:
......+...
..........
..........
..........
....#...##
....#...#.
..###...#.
......o.#.
....oooo#.
#########.
After a total of 22 units of sand:
......+...
..........
......o...
.....ooo..
....#ooo##
....#ooo#.
..###ooo#.
....oooo#.
...ooooo#.
#########.
Finally, only two more units of sand can possibly come to rest:
......+...
..........
......o...
.....ooo..
....#ooo##
...o#ooo#.
..###ooo#.
....oooo#.
.o.ooooo#.
#########.
Once all 24 units of sand shown above have come to rest, all further sand flows out the bottom, falling into the endless void. Just for fun, the path any new sand takes before falling forever is shown here with ~:
.......+...
.......~...
......~o...
.....~ooo..
....~#ooo##
...~o#ooo#.
..~###ooo#.
..~..oooo#.
.~o.ooooo#.
~#########.
~..........
~..........
~..........
Using your scan, simulate the falling sand. How many units of sand come to rest before sand starts flowing into the abyss below?
Part 2
You realize you misread the scan. There isn’t an endless void at the bottom of the scan - there’s floor, and you’re standing on it!
You don’t have time to scan the floor, so assume the floor is an infinite horizontal line with a y coordinate equal to two plus the highest y coordinate of any point in your scan.
In the example above, the highest y coordinate of any point is 9, and so the floor is at y=11. (This is as if your scan contained one extra rock path like -infinity,11 -> infinity,11.) With the added floor, the example above now looks like this:
...........+........
....................
....................
....................
.........#...##.....
.........#...#......
.......###...#......
.............#......
.............#......
.....#########......
....................
<-- etc #################### etc -->
To find somewhere safe to stand, you’ll need to simulate falling sand until a unit of sand comes to rest at 500,0, blocking the source entirely and stopping the flow of sand into the cave. In the example above, the situation finally looks like this after 93 units of sand come to rest:
............o............
...........ooo...........
..........ooooo..........
.........ooooooo.........
........oo#ooo##o........
.......ooo#ooo#ooo.......
......oo###ooo#oooo......
.....oooo.oooo#ooooo.....
....oooooooooo#oooooo....
...ooo#########ooooooo...
..ooooo.......ooooooooo..
#########################
Using your scan, simulate the falling sand until the source of the sand becomes blocked. How many units of sand come to rest?
Code
defmodule Day14 do
def parse_input(input) do
for l <- String.split(input, "\n", trim: true), reduce: {0, [], %{}} do
acc ->
{ymax, list, map} = acc
{ymax, line, new_map} =
for p <- String.split(l, " -> ", trim: true), reduce: {ymax, [], map} do
acc ->
{ymax, list, map} = acc
[x, y] =
p
|> String.split(",")
|> Enum.map(&String.to_integer/1)
new_map = Map.update(map, x, %{}, fn v -> v end)
new_ymax =
cond do
y > ymax -> y
true -> ymax
end
{new_ymax, list ++ [[x, y]], put_in(new_map[x][y], "#")}
end
{ymax, list ++ [line], new_map}
end
end
defp generate_map({ymax, lines, map}) do
# generate map of walls
# map is a Map of maps
for l <- lines, reduce: {ymax, map} do
acc ->
{_, map_to_reduce} = acc
[p | t] = l
{_, new_map} =
for end_point <- t, reduce: {p, map_to_reduce} do
acc ->
{start_point, map} = acc
# draw a straight line from start point to end point
new_map = draw_line(start_point, end_point, map)
{end_point, new_map}
end
{ymax, new_map}
end
end
defp draw_line(start_point, end_point, map) do
# draw a straight line and make it a wall
[xs, ys] = start_point
[xe, ye] = end_point
# IO.puts("Drawing line from (#{xs}, #{ys}) to (#{xe}, #{ye})")
new_map =
cond do
# vertical line
xs == xe ->
[y1, y2] = Enum.sort([ys, ye])
for y <- (y1 + 1)..(y2 - 1), reduce: map do
acc -> put_in(acc[xs][y], "#")
end
# horizontal line
ys == ye ->
[x1, x2] = Enum.sort([xs, xe])
for x <- (x1 + 1)..(x2 - 1), reduce: map do
acc ->
mx =
case Map.has_key?(acc, x) do
false -> Map.put(acc, x, %{})
true -> acc
end
put_in(mx[x][ys], "#")
end
# diagonal line
true ->
:error
end
new_map
end
defp sand_pouring(part_n, ymax, map, point) do
{x, y} = point
cond do
y >= ymax and part_n == 1 ->
{:halt1, map}
y == ymax + 1 and part_n == 2 ->
mx =
case Map.has_key?(map, x) do
false -> Map.put(map, x, %{})
true -> map
end
{:cont, put_in(mx[x][y], "o")}
map[x][y + 1] == nil ->
sand_pouring(part_n, ymax, map, {x, y + 1})
map[x - 1][y + 1] == nil ->
sand_pouring(part_n, ymax, map, {x - 1, y + 1})
map[x + 1][y + 1] == nil ->
sand_pouring(part_n, ymax, map, {x + 1, y + 1})
true ->
cond do
part_n == 2 and x == 500 and y == 0 -> {:halt2, map}
true -> {:cont, put_in(map[x][y], "o")}
end
end
end
defp sand_pouring_cycle(part_n, cycle, ymax, map) do
# IO.puts("cycle #{cycle}")
{status, map} = sand_pouring(part_n, ymax, map, {500, 0})
case status do
:cont -> sand_pouring_cycle(part_n, cycle + 1, ymax, map)
:halt1 -> {cycle - 1, map}
:halt2 -> {cycle, map}
end
end
def part(input, part_n) do
{ymax, map} =
input
|> parse_input()
|> generate_map
{num_cycles, _final_map} = sand_pouring_cycle(part_n, 1, ymax, map)
num_cycles
end
end
{:module, Day14, <<70, 79, 82, 49, 0, 0, 31, ...>>, {:part, 2}}
Checking that the example input produces the right result:
Day14.part(example_input, 1) == 24
true
input = File.read!("Day14/input.txt")
Day14.part(input, 1)
799
Day14.part(example_input, 2) == 93
true
Using real input data (saved on a file as it’s too long to show here):
Day14.part(input, 2)
29076