GitHub

Day 04 - Solver

Solver Code (src/solvers/day04.ts):

/**
 * Solver for Day 4 of Advent of Code 2025
 *
 * To use this solver:
 * 1. Implement the solve function below
 * 2. The function receives the puzzle input as a string
 * 3. Return the solution (can be a string, number, or object)
 */

const getSurroundingRolls = (
  grid: Array<Array<string>>,
  row: number,
  col: number,
): number => {
  // 8 directions: up, down, left, right, and 4 diagonals
  const directions = [
    [-1, -1],
    [-1, 0],
    [-1, 1], // top row
    [0, -1],
    [0, 1], // middle row (skip center)
    [1, -1],
    [1, 0],
    [1, 1], // bottom row
  ]

  let surroundingRolls = 0
  for (const [dr, dc] of directions) {
    const newRow = row + dr
    const newCol = col + dc

    // Check bounds
    if (
      newRow >= 0 &&
      newRow < grid.length &&
      newCol >= 0 &&
      newCol < grid[0].length &&
      grid[newRow][newCol] === '@'
    ) {
      surroundingRolls++
    }
  }
  return surroundingRolls
}

export function solve(input: string): Promise<string | number | object> {
  const grid = input.split('\n').map((line) => line.split(''))
  const rows = grid.length
  const cols = grid[0].length
  let rollsPt1 = 0

  for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
    const row = grid[rowIndex]
    for (let colIndex = 0; colIndex < cols; colIndex++) {
      const cell = row[colIndex]
      if (cell === '@') {
        const surroundingRolls = getSurroundingRolls(grid, rowIndex, colIndex)
        if (surroundingRolls < 4) {
          rollsPt1++
          grid[rowIndex][colIndex] = 'x'
        }
      }
    }
  }
  // Part 2: Iteratively remove rolls until no more can be removed
  // Part 2 total includes part 1, so start with rollsPt1
  let totalRollsRemoved = rollsPt1
  let rollsRemovedThisIteration = 1 // Start with 1 to enter the loop

  while (rollsRemovedThisIteration > 0) {
    rollsRemovedThisIteration = 0

    for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
      const row = grid[rowIndex]
      for (let colIndex = 0; colIndex < cols; colIndex++) {
        const cell = row[colIndex]
        if (cell === '@') {
          const surroundingRolls = getSurroundingRolls(grid, rowIndex, colIndex)
          if (surroundingRolls < 4) {
            rollsRemovedThisIteration++
            totalRollsRemoved++
            grid[rowIndex][colIndex] = 'x'
          }
        }
      }
    }
  }

  return Promise.resolve({
    part1: rollsPt1,
    part2: totalRollsRemoved,
  })
}

How to add your solver:

Create a file at src/solvers/day04.ts with the following structure:

export async function solve(input: string): Promise<string | number | object> {
  // Your solution here
  // The input parameter contains the puzzle input as a string
  
  // Example:
  const lines = input.trim().split('\n');
  
  // Process and return your answer
  return 'Your answer here';
}

Then, import it in src/solvers/index.ts and add it to the solvers object:

import * as day04 from './day04'

export const solvers = {
  // ... existing solvers
  '04': day04,
}

The solver function will receive the puzzle input as a string and should return the solution (string, number, or object).

Day 04 - Advent of Code 2025

--- 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?

Your puzzle answer was 1523.

The first half of this puzzle is complete! It provides one gold star: *

--- 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?

Answer:

Although it hasn't changed, you can still get your puzzle input.

You can also [Shareon Bluesky Twitter Mastodon] this puzzle.