Day 4 - Advent of Code 2025
Mix.install([:kino, :benchee])
Links
Prompt
— Day 4: Printing Department —
You ride the escalator down to the printing department. They’re clearly getting ready for Christmas; they have lots of large rolls of paper everywhere, and there’s even a massive printer in the corner (to handle the really big print jobs).
Decorating here will be easy: they can make their own decorations. What you really need is a way to get further into the North Pole base while the elevators are offline.
“Actually, maybe we can help with that,” one of the Elves replies when you ask for help. “We’re pretty sure there’s a cafeteria on the other side of the back wall. If we could break through the wall, you’d be able to keep moving. It’s too bad all of our forklifts are so busy moving those big rolls of paper around.”
If you can optimize the work the forklifts are doing, maybe they would have time to spare to break through the wall.
The rolls of paper (@) are arranged on a large grid; the Elves even have a helpful diagram (your puzzle input) indicating where everything is located.
For example:
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
The forklifts can only access a roll of paper if there are fewer than four rolls of paper in the eight adjacent positions. If you can figure out which rolls of paper the forklifts can access, they’ll spend less time looking and more time breaking down the wall to the cafeteria.
In this example, there are 13 rolls of paper that can be accessed by a forklift (marked with x):
..xx.xx@x.
x@@.@.@.@@
@@@@@.x.@@
@.@@@@..@.
x@.@@@@.@x
.@@@@@@@.@
.@.@.@.@@@
x.@@@.@@@@
.@@@@@@@@.
x.x.@@@.x.
Consider your complete diagram of the paper roll locations. How many rolls of paper can be accessed by a forklift?
To begin, get your puzzle input.
— Part Two —
Now, the Elves just need help accessing as much of the paper as they can.
Once a roll of paper can be accessed by a forklift, it can be removed. Once a roll of paper is removed, the forklifts might be able to access more rolls of paper, which they might also be able to remove. How many total rolls of paper could the Elves remove if they keep repeating this process?
Starting with the same example as above, here is one way you could remove as many rolls of paper as possible, using highlighted @ to indicate that a roll of paper is about to be removed, and using x to indicate that a roll of paper was just removed:
Initial state:
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
Remove 13 rolls of paper:
..xx.xx@x.
x@@.@.@.@@
@@@@@.x.@@
@.@@@@..@.
x@.@@@@.@x
.@@@@@@@.@
.@.@.@.@@@
x.@@@.@@@@
.@@@@@@@@.
x.x.@@@.x.
Remove 12 rolls of paper:
.......x..
.@@.x.x.@x
x@@@@...@@
x.@@@@..x.
.@.@@@@.x.
.x@@@@@@.x
.x.@.@.@@@
..@@@.@@@@
.x@@@@@@@.
....@@@...
Remove 7 rolls of paper:
..........
.x@.....x.
.@@@@...xx
..@@@@....
.x.@@@@...
..@@@@@@..
...@.@.@@x
..@@@.@@@@
..x@@@@@@.
....@@@...
Remove 5 rolls of paper:
..........
..x.......
.x@@@.....
..@@@@....
...@@@@...
..x@@@@@..
...@.@.@@.
..x@@.@@@x
...@@@@@@.
....@@@...
Remove 2 rolls of paper:
..........
..........
..x@@.....
..@@@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@x.
....@@@...
Remove 1 roll of paper:
..........
..........
...@@.....
..x@@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@..
....@@@...
Remove 1 roll of paper:
..........
..........
...x@.....
...@@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@..
....@@@...
Remove 1 roll of paper:
..........
..........
....x.....
...@@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@..
....@@@...
Remove 1 roll of paper:
..........
..........
..........
...x@@....
...@@@@...
...@@@@@..
...@.@.@@.
...@@.@@@.
...@@@@@..
....@@@...
Stop once no more rolls of paper are accessible by a forklift. In this example, a total of 43 rolls of paper can be removed.
Start with your original diagram. How many rolls of paper in total can be removed by the Elves and their forklifts?
Although it hasn’t changed, you can still get your puzzle input.
Input
input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"..@@.@@@@.\n@@@.@.@.@@\n@@@@@.@.@@\n@.@@@@..@.\n@@.@@@@.@@\n.@@@@@@@.@\n.@.@.@.@@@\n@.@@@.@@@@\n.@@@@@@@@.\n@.@.@@@.@."
Solution
defmodule Grid do
defstruct grid: %{}, max_x: nil, max_y: nil
end
defmodule Day04 do
defdelegate parse(input), to: __MODULE__.Input
def part1(input) do
grid = parse(input).grid
Enum.count(grid, &removable_roll?(&1, grid))
end
def part2(input) do
grid = parse(input).grid
grid
|> remove_rolls()
|> Enum.count(fn {_, char} -> char == ?x end)
end
@allowed_adjacent_rolls 3
defp removable_roll?({xy, ?@}, grid) do
xy
|> adjacent_positions()
|> Enum.count(fn xy ->
Map.get(grid, xy) == ?@
end)
|> then(&(&1 <= @allowed_adjacent_rolls))
end
defp removable_roll?(_, _), do: false
defp remove_rolls(grid) do
new_grid =
grid
|> Enum.map(fn {xy, char} ->
if removable_roll?({xy, char}, grid),
do: {xy, ?x},
else: {xy, char}
end)
|> Map.new()
if new_grid == grid, do: new_grid, else: remove_rolls(new_grid)
end
defp adjacent_positions({x, y}) do
[
{x - 1, y - 1},
{x - 1, y},
{x - 1, y + 1},
{x, y + 1},
{x + 1, y + 1},
{x + 1, y},
{x + 1, y - 1},
{x, y - 1}
]
end
defmodule Input do
def parse(input) when is_binary(input) do
input
|> String.splitter("\n", trim: true)
|> parse()
end
def parse(input) do
Stream.map(input, &String.to_charlist/1)
|> Enum.with_index()
|> Enum.reduce(%Grid{}, fn {line, x}, acc ->
line
|> Enum.with_index()
|> Enum.reduce(acc, fn {char, y}, acc ->
acc
|> Map.update!(:grid, &Map.put(&1, {x, y}, char))
|> Map.put(:max_y, y)
end)
|> Map.put(:max_x, x)
end)
end
end
end
{:module, Day04, <<70, 79, 82, 49, 0, 0, 17, ...>>,
{:module, Day04.Input, <<70, 79, 82, ...>>, {:parse, 1}}}
Consider your complete diagram of the paper roll locations. How many rolls of paper can be accessed by a forklift?
Your puzzle answer was 1397.
Day04.part1(input)
13
Start with your original diagram. How many rolls of paper in total can be removed by the Elves and their forklifts?
Your puzzle answer was 8758.
Day04.part2(input)
43
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
Tests
ExUnit.start(auto_run: false)
defmodule Day04Test do
use ExUnit.Case, async: false
setup_all do
[
input: "..@@.@@@@.\n@@@.@.@.@@\n@@@@@.@.@@\n@.@@@@..@.\n@@.@@@@.@@\n.@@@@@@@.@\n.@.@.@.@@@\n@.@@@.@@@@\n.@@@@@@@@.\n@.@.@@@.@."
]
end
describe "part1/1" do
test "returns expected value", %{input: input} do
assert Day04.part1(input) == 13
end
end
describe "part2/1" do
test "returns expected value", %{input: input} do
assert Day04.part2(input) == 43
end
end
end
ExUnit.run()
Running ExUnit with seed: 793042, max_cases: 28
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures
%{total: 2, failures: 0, excluded: 0, skipped: 0}
Benchmarking
defmodule Benchmarking do
# https://github.com/bencheeorg/benchee
def run(input) do
Benchee.run(
%{
"Part 1" => fn -> Day04.part1(input) end,
"Part 2" => fn -> Day04.part2(input) end
},
memory_time: 2,
reduction_time: 2
)
nil
end
end
{:module, Benchmarking, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:run, 1}}
Benchmarking.run(input)
Operating System: macOS
CPU Information: Apple M4 Pro
Number of Available Cores: 14
Available memory: 48 GB
Elixir 1.18.3
Erlang 27.3
JIT enabled: true
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: none specified
Estimated total run time: 22 s
Excluding outliers: false
Benchmarking Part 1 ...
Benchmarking Part 2 ...
Calculating statistics...
Formatting results...
Name ips average deviation median 99th %
Part 1 127.00 7.87 ms ±5.78% 7.69 ms 9.06 ms
Part 2 3.62 275.91 ms ±0.78% 275.60 ms 279.97 ms
Comparison:
Part 1 127.00
Part 2 3.62 - 35.04x slower +268.04 ms
Memory usage statistics:
Name average deviation median 99th %
Part 1 17.33 MB ±0.00% 17.33 MB 17.33 MB
Part 2 307.97 MB ±0.29% 307.87 MB 309.23 MB
Comparison:
Part 1 17.33 MB
Part 2 307.97 MB - 17.77x memory usage +290.64 MB
Reduction count statistics:
Name average deviation median 99th %
Part 1 1.31 M ±0.01% 1.31 M 1.31 M
Part 2 48.56 M ±0.04% 48.56 M 48.60 M
Comparison:
Part 1 1.31 M
Part 2 48.56 M - 37.06x reduction count +47.25 M
nil