1 vote

Day 4: Printing Department

Today's problem description: https://adventofcode.com/2025/day/4

Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python with any of the "short names" listed in this page of supported languages):

<details>
<summary>Part 1</summary>

```python
Your code here.
```

</details>

3 comments

  1. fidwell
    Link
    Finally, a character grid problem. If you've done one of these before, hopefully you've saved up some utility methods from previous years to solve this one in a snap. Thoughts My old utility code...

    Finally, a character grid problem. If you've done one of these before, hopefully you've saved up some utility methods from previous years to solve this one in a snap.

    Thoughts

    My old utility code already had methods for (1) parsing the input into an array of chars; (2) give you an IEnumerable of all coordinates in the grid; (3) find the coordinates of the eight neighbors of a coordinate; and (4) replace the character at a coordinate. All I had to write is checking if the neighbor count of `@` is less than 4., and write a loop to replace them until we can't anymore. Nothing really challenging here, and the code runs mighty fast without the need for any micro-optimizations.

    C# code

    1 vote
  2. jzimbel
    (edited )
    Link
    Elixir I've built up a pretty robust Grid module over the years—maybe concerningly robust given that it's used solely for a bunch of toy code puzzles—so today's solution was a quick one. No...

    Elixir

    I've built up a pretty robust Grid module over the years—maybe concerningly robust given that it's used solely for a bunch of toy code puzzles—so today's solution was a quick one.

    No special optimizations, I just took the obvious approach.

    Both parts
    defmodule AdventOfCode.Solution.Year2025.Day04 do
      alias AdventOfCode.Grid, as: G
    
      use AdventOfCode.Solution.SharedParse
    
      @impl true
      def parse(input), do: G.from_input(input)
    
      def part1(grid), do: map_size(forklift_cells(grid))
    
      def part2(grid) do
        grid
        |> Stream.unfold(&remove_forkliftable/1)
        |> Enum.sum()
      end
    
      defp forklift_cells(grid) do
        for {coords, ?@} <- grid, forkliftable?(coords, grid), into: %{}, do: {coords, ?.}
      end
    
      defp forkliftable?(coords, grid) do
        grid
        |> G.adjacent_values(coords)
        |> Enum.count(&(&1 == ?@))
        |> Kernel.<(4)
      end
    
      defp remove_forkliftable(grid) do
        removals = forklift_cells(grid)
    
        case map_size(removals) do
          # End the stream.
          0 -> nil
          n -> {n, G.replace(grid, removals)}
        end
      end
    end
    
    Benchmarks
    Name             ips        average  deviation         median         99th %
    Parse         526.68        1.90 ms     ±1.87%        1.90 ms        1.97 ms
    Part 1        159.39        6.27 ms     ±3.94%        6.27 ms        6.95 ms
    Part 2          7.69      130.08 ms     ±0.99%      129.91 ms      134.38 ms
    
    Bonus: Final grid I thought the grid looked neat after all of the forkliftable cells had been cleared.

    This is what I get for my puzzle input, it's pleasingly blobby:

    ...........................................................................................................................................
    .............................................................................@@............................................................
    ............................................................................@@@@...........................................................
    ...........................................................................@@.@@@..........................................................
    ...........................................................................@@.@@@@@........................................................
    ...........................................................................@@..@@@@@.......................................................
    ............................................................................@@@@@@@@.......................................................
    .............................................................................@.@@.@@...@@..................................................
    ..............................................................................@@@@@@..@@@@.................................................
    ......................@@@@@...............@@...................................@@@@@@@.@@@@................................................
    .....................@@@@@@@.............@@@@...................................@@..@@@@@@@................................................
    ....................@@..@@@@.............@@@@........................................@.@@@@................................................
    ...................@@....@@...............@..@........................................@@@@........................@@@......................
    .......@@......@@.@@@......................@@@@.......................................@.@........................@@@@@.....................
    ......@@@@....@@@@@.@......................@@@@......................................@@@........................@@@@.@@....................
    ......@@@@@@.@@@.@.@.@@.....................@@.......................................@@...........@@............@@.@.@@....................
    ......@@@@@@@.@@@@@@@@@@@@............................................................@..........@@@@...........@@@.@@@....................
    .....@@@@@@@@@@.@@@@@@..@@@...........................................................@@........@@@@@@@@.........@.@@@.....................
    ....@@.@@..@.@.@@@@@.@.@@@@.......................................@@.................@@@.......@.@@@..@@@.........@@@@.....................
    ....@@.@@..@@@@@@.@.@@.@@@@......................@@....@@@@.....@@@@@................@@@......@@@@@....@@@.........@@......................
    ....@@@@@@@@..@@@.@@@@.@@@......................@@@@..@@@@@@@@.@@@@@.@................@@......@@.@@@...@@@@................................
    ....@@....@.@@@..@@@.@@@@.......................@@@@.@@@@@@@@@@.@..@@@@..............@@......@@@@@@@....@@@................................
    ...@@@.....@@..@@.@@@.@.@........................@@@@@@.....@.@@@@@.@@.@............@@...@@@@@@@.@@@@@@@@@@................................
    ..@@@......@@@@@@@@.@@@@.@.........................@.@@....@..@@@@@...@@@...........@@..@@@@@@@....@@@@..@...........@@....................
    ..@@........@@.@@@@@@@@.@@@........................@@.@...@@@.@.@@.....@@............@..@@@...........@@@..........@@@@@@..................
    ..@@@...........@@..@@.@@@@........................@@.@@.@@@.@@@@@.....@@@........@@@@.@..............@@@....@@...@@@@@@@@.................
    .@@@@.................@@@@..........................@@@@@@.@.@@@@......@.@@......@@@@@@@@.............@@....@@@@.@.@@@@@.@@................
    @@.@...................@.............................@...@@@@.@@@....@@@@@@......@@.@@@@@.............@....@@.@@@@@@@....@@@...............
    @@@@.................@@........................@@....@@@@@.@....@@@.@@@.@@@.......@@.@.@@............@@@@.@@@@@@@.@@@@....@@...............
    @@@@................@@@@......................@@@@..@@@@@@@.....@@@@@@..@@........@@@.@@@............@@@@@.@@@.....@@@......@..............
    .@@@@...............@@@@.....................@@..@@.@@..@@.......@@@@...@@@@.....@@.@@.@@.............@@@...@.....@@@@@@..@@@@.............
    ..@@@................@@...................@@@@.@@.@@..............@@.....@@@@....@@@@.@@............@@@@.....@@@@@@..@@@@@@@@@.............
    ...@@....................................@@@..@@@..@....................@@.@@....@.@.@.@...........@@@@.....@@@@@@@@...@.@@.@..............
    ..@.@@...................................@@.@@.@@@@@....................@@@@@@@.@@.@.@@@@.........@@.@......@@@@.@@@...@.@@@...............
    .@@@@@.................................@@@@.@@@@@.@@.....................@@@.@@@@@@@@@@@@.........@@@@@......@@@@@@@..@@@@@................
    .@@@@.................................@@@@@..@@@@.@............................@@@.@...@..........@@@@@.......@.@@@@..@@@@.................
    ..@@@...............................@@@@.@....@@..@@...@@......................@@@@@@.@............@@.@@.......@@...@@@.........@@@@@@.....
    .@@@...............................@@@@.@@@...@@@@@@@.@@@@......................@@@.@@@@@............@@@.......@...@@@@.@@.@@..@@@@@@@@@@..
    .@@@..............................@.@@@@@@@@.@@@@.@@@@.@@@@..........................@.@@@...........@@@.......@@..@@.@@@@@@@@@@..@...@@@@.
    .@@.............................@@@@@.@@@@@@@@@@@@@@@@@@.@.@@.........................@..@@...........@........@@@.@@@@@.@@@.@.@.@@@..@@@@.
    .@.............................@@@@@...@@.@@.....@@@@@@@@@.@@@.......................@@@@@@..........@.........@@.@@@@@@.@@.@.@@@@@@@@@@@@.
    .@@..@@................@@.....@@@@@...............@@@@@@@@.@@@......................@@.@@@..........@@@@@.....@@@@@@@.@@@@@@@@@@.@@@@.@@@..
    .@@@@@@@..............@@@@...@@@@.@.................@@@@..@@@.......................@@@.@...........@@@@@@....@@.@@@..@@@@@@.@@............
    .@@@.@@@..............@@.@@..@@.@@@@....................@@@........................@.@@@@..........@@@@@@@....@@@@@@@@@@.@@................
    ..@@@@@@...............@@.@@.@@.@@@@@..@@@..............@@........................@@@.@@@..........@@@@.@......@@@@@@@@@@@.................
    .....@@................@@@@@@@@@.@@@.@@@@@@...........@@........................@@.@@@@@...........@@.@@............@@@.@@@................
    ......................@@@@@..@@@.@@@@@@@@@@..........@@@.......................@@@@@@@@............@@..@@.............@@.@@................
    .....................@@@..@@.@@@@.@@@@@.@.@@.......@@.@.......................@@@@@.................@@@@@..............@@@@................
    .....................@@@.@@@@@@@@@...@@@@@.@@.@@@.@@@@@@.....................@@@@@.....@@............@@@................@@.................
    ......................@@@@@@..@@@......@@@@@.@@@@@.@.@@@....................@.@.@.....@@@@.................................................
    .......................@@@@@.@@@@...........@@@.@...@.@@@..................@@@@@......@@@@..................................@@.............
    ......................@@...@@.@@.............@@@.@.@@@@@@@.................@@@@........@@..................................@@@@............
    .....................@@.....@@..@@@...@@.......@@@@@@.@@@@.................@.@.............................................@@@@............
    .....................@@@...@@.@@@@@@@@@@@.......@@....@@@@................@@@@@.............................................@@.............
    ..................@@@@@@@..@@@@@@@@@@.@.@@.............@@.................@@@@@............................................................
    ................@@@@@.@@.@@..@@@@@...@@@@@..............@..................@@@.............................................................
    ...............@@@@..@@@.@@@.........@@.@...............@@.................................................................................
    ...............@@@@..@@@@.@@..........@@@@..............@@.................................................................................
    ......@@......@@@@....@..@@............@@@@.....@@@......@@....@@@.........................................................................
    .....@@@@.....@@@@.....@@@............@@@@@@...@@@@@.@@.@@@.@@@@@@@........................................................................
    .....@@@@......@@.....@@@.............@@@@@@@.@@.@@.@@@@@@@@@@@.@@@........................................................................
    ......@@..............@@@@.............@@@@.@@@@.@@.@@@@.....@@@@@.........................................................................
    ......................@@@@................@@@@.@@.@@.@@........@@@..@@.....................................................................
    .......................@@@................@@@@.@@@@@@..........@@@@@@@@....................................................................
    ........................@@.................@@@@@@@@@...........@@.@@@@@@............................................................@@@@@..
    .......................@@@...................@@@@@.............@@@@@@@.@@.....................................................@@.@@@@@@@@@.
    .......................@@@.....................@@@@............@@@@.@@@@@....................................................@@@@@@@@...@@@
    ........................@.@@....................@@@.............@@@@@.@@@@.............................................@@@..@@@.@@@.@@@@.@@
    .........................@@@@.....................@@..............@@....@@............................................@@@@@.@@@@@@@.@.@@..@
    ..........................@@@....................@@@....................@..............................@@............@.@@@@@.@@@.@@@@@@.@@@
    ............................@@..................@@@.....................@@............................@@@@........@@@@@@@@@.......@@@@@@.@@
    .............................@@.................@@......................@@@..........................@@@@@......@@@@..@@@...........@@.@@@@
    ............................@@@@@....@@........@@@.......................@@.........................@@.@@@@....@@@@.....................@@@
    ............................@@@@@@..@@@@@@.....@@@.......................@@@......................@@@@@@.@.@@@@@..........................@
    .............................@@@@@..@@@@@@@....@@@@......................@@@@.@@@@...............@@@@@.@@@@@@@@..........................@@
    ............................@@@@@...@@@@@@@@...@@@.@@.....................@@.@@@@@@..............@@....@..@.@@...........................@@
    ......................@@.@@@.@@@...@@@@@@.@@@..@@@@@@@...........@@......@@@@..@@@.@@@.....@@@..@@@....@@@@@@.@.........................@@@
    .....................@@@@@@@.@..@@.@@@@@@@@@@...@@@@@@..........@@@@....@@@@.@@@@@@@@@@...@@@@@@.@....@@@..@@@@@........................@@@
    .....................@@@@.@@@.@@@@@.@@@@..@@...@@.@@..@.........@@.@@..@@@@@@@@@@.@@@@@...@@.@@@@@@..@@@@@@.@@@@@.......................@@.
    ......................@@...@@@.@.@@@@..........@@@.@@@@@.......@@@@@@.@@.@.@@.@@@@.@@@@....@@@@@@@@.@.@@.@@@@@@@@@@...................@@@..
    ............@@@............@@@@.@@.@@..........@@@@@@@@@@......@@@@.@@.@@@@.@@@@@@@@.@@...@@@@@..@.@@@@@@..@@@@@.@@@@@@..............@@@...
    ...........@@@@@............@@@@.@@@..........@@.@@@@@@@.@@.....@@@@@@.@..@@....@@@.@@@@.@@.@@@.@.@@@@@.@@@@@.....@@@@@@.............@@....
    ...........@@@@@................@@@@@.........@@@.@@@.@..@@@......@@@@@.@@@@.....@@@.@@@@@@@@@@@@@@@@@...@.@@.......@.@@.........@@@@......
    ...@@......@@@@@.................@@@@.........@@@@@@@@.@@@@@.......@@@.@.@@.....@@@......@@@@...@.@@@.....@@@....@@@@@@@...@@...@@@@@......
    ..@@@@@@@...@@@...................@@...........@@@@..@@.@@@@.......@@@@.@@@....@@@..............@@@@.......@@@@@@@@@.@@@..@@@@.@@.@@.......
    ..@@@@@@@@...........................................@.@@@@........@@@.@@@@....@@@..............@@@.@@........@@@.....@@@@@.@.@@.@@@.......
    ..@@.@@@.@@..........................................@@@@...........@@.@.@......@@..............@@@@@@@................@@..@@@@@@@@........
    ...@@@@@@@@..........................................@@@@.............@@@@@.....@@...............@@@@@@.....................@@@@@..@.......
    ....@..@@@...........................................@@@..............@@.@.@@@@@@@................@@@@..........................@@@@@......
    .....@@@@@................@@.........................@@...............@@@@@@.@@@@@.................@@...........................@..@@......
    ....@@@@@@...............@@@@........................@@@..............@@@@@.......@............................................@@@@@.......
    ....@@@..@...............@@.@@........................@@@@.............@@........@@@...........................................@@@@........
    .....@@@@@@...............@@@@........................@@@@@@.....................@@@@@@........................................@@@@@.......
    ....@@.@@@.@...............@@.........................@@@.@@@.....................@.@@@@@@@@...........@@......................@@.@@.......
    ....@@@@@@@@@.........................................@@.@@@@@.....................@@.@@@.@@@.........@@@@.....................@@@@........
    ...@@@@...@@@..........................................@@@@@@.@....................@@@@@..@@@.........@@@@......................@@.........
    ..@@@.....@@............................................@@@@@@@@...................@@@.....@.........@@@@@.................................
    ..@@@@@..@@...................................................@@@...................@@...@@..........@@.@..................................
    ..@@@@@@@.@....................................................@.@...................@@@@@@...........@@@@@@@..............................
    ...@@@@@@@@.....................................................@@@...................@.@@.............@@.@@@@.............................
    ..@@.@@@@@@@@............................................@@@.@@.@@@...........@@@....@@@.................@@.@@.............................
    .@@@@@.@@@@@@@@.........................................@@@@@@@@@.@..........@@@@@...@@...................@@@@@............................
    .@@@@....@.@.@@@........................................@@@....@.@@.........@@.@@@@@@@@....................@@.@@...........................
    ..@@.....@@..@@@@@.................................@@....@@@...@@@@........@@@@@.@@.@@@........................@@..........................
    .........@@...@@@@@@..............................@@@@...@@.@..@@@.........@@.@@@@@@.@.........................@@..........................
    ..........@@@@@@..@@@..........................@@@@.@@@..@@@@@.@@.........@@@@.@.@@@@.........................@@@..........................
    ....@@....@.@@...@@@@.........................@@@..@@@@.@@@@.@@@.........@@.@.@.@@..@@.....................@@@@@...........................
    ...@@@@...@@.....@@@@.........................@@@...@@@@@.@@@@@..........@@@.@@..@..@.@...................@@@@@............................
    ..@.@@@..@@.......@@....@@....................@@......@@@................@@@@@@@@@.@@@@@..................@@@@.............................
    .@@@..@..@@@...........@@@@....................@......@@@................@.@@@.@.@@@@.@@...................@@..............................
    @@@@@@@..@@@..........@@.@@...................@@.....@@@@................@@@@@@.@@@...@....................................................
    @@@.@@.@.@@...........@@..@..................@@@@@@@@@@.@@...............@@....@@.@@@@@....................................................
    @@@@.@@@@@............@@@@@..................@@..@@.@@@@@@..............@@@....@@@@@@@@....................................................
    .@@...@@@..............@@@@..................@.........@@...............@@@.....@@@@@@.....................................................
    ......................@@@@...................@@.........................@@......@@.@@......................................................
    ......................@@@.....@@@............@@.....................@@...@.......@@........................................................
    .......................@@@@@.@@@@@@.........@@...................@@@@@@@@@@@@...@@@...................@@@..................................
    ........................@@@@@@@.@@@@@.......@@@@........@@......@@@..@@@@@@@@@@@@@...................@@@@@.........................@@......
    ............................@.@@.@.@@@.......@@@@......@@@@.....@@@@.@@@@@.@@@@.@@..................@@@@@@@@@.....................@@@@.....
    .............................@..@@@@@@@........@@@.....@@.@@.....@.@@@@.....@@@.@...................@@@..@.@@@....................@@@@.....
    .............................@@@@...@@@.........@@.....@@.@@@@@@.@@...........@@@@..................@@.@.@@@@@....................@@@@.....
    .............................@@@@@..@@@........@@......@@@@@@@@@@@.............@@@...................@@@@@@@@.................@@@.@@@......
    ............................@@@@@@...@........@@@.......@.@@@..@@@@.............@@....................@@@@...................@@@@@.........
    ............................@@@@@...@@@.......@@.........@@@..@.@@@............@@@@....................@@....................@@.@@.........
    .............................@@@@@@.@@@.......@@@.........@.@@@@..@............@@@@..........................................@@@@..........
    ..............................@.@@@@@@.......@.@@..........@@.@@@@@.............@@............................................@@...........
    ...............................@@@..........@@@@...........@@@..@@@........................................................................
    ................................@@......@@.@@@@.............@@@@@@............................................................@@...........
    ..................................@@...@@@@@@................@@@@............................................................@@@@..........
    ..................................@@@..@@@@@..............................................................................@@@@@@@.@@@@@@...
    ..................................@@@...@@@@@............................................................................@@@..@@@@@@@.@@@..
    ...................................@@@..@@@@@@..............................................@@..........................@@...@@@@@@@@@.@@..
    ...................................@@.@@@.@@@@...............................@@............@@@@.........................@@@@@@.....@@@@@...
    ..................................@@..@@@..@@...............................@@@@@..........@@@@........................@@..@@@.....@@@@....
    .................................@@@@@@@....................................@@@@@@..........@@.......@@................@@@@.@@......@@.....
    ................................@@@.@@.......................................@@@@@..................@@@@................@@@@@@.............
    ................................@@@@@..........................................@@...................@@@@@..................@@..............
    ................................@@@@@................................................................@@@@..................................
    .................................@@@..................................................................@@...................................
    
    1 vote
  3. scarecrw
    (edited )
    Link
    Over two hours to write and over a minute to run... Yesterday I would have said Prolog didn't seem so bad and I was getting a feel for things, today I spent the better part of an hour getting a...

    Over two hours to write and over a minute to run...

    Yesterday I would have said Prolog didn't seem so bad and I was getting a feel for things, today I spent the better part of an hour getting a Moore neighborhood to work.

    Real tired and pretty disappointed after this one. Hopefully get a second wind with some free time this weekend.

    Prolog Solution
    :- ['src/util.pl'].
    :- table contains_paper/3.
    
    contains_paper(Grid, 0, Position):-
        Position = (RowIndex, ColIndex),
        nth0(RowIndex, Grid, Row),
        string_chars(Row, RowChars),
        nth0(ColIndex, RowChars, Char),
        Char = '@'.
    
    contains_paper(Grid, Step, Position):-
        succ(Step1, Step),
        contains_paper(Grid, Step1, Position),
        not(forkliftable(Grid, Step1, Position)).
    
    forkliftable(Grid, Step, Position):-
        contains_paper(Grid, Step, Position),
        findall(Pos, neighbor(Position, Pos), Neighbors),
        count(contains_paper(Grid, Step), Neighbors, NeighborPaper),
        NeighborPaper < 4.
    
    neighbor(Position1, Position2):-
        Position1 = (Row1, Col1),
        between(-1, 1, DR),
        between(-1, 1, DC),
        (DR, DC) \= (0, 0),
        Row2 is Row1 + DR,
        Col2 is Col1 + DC,
        Position2 = (Row2, Col2).
    
    paper_removed(Grid, Step, PaperRemoved):-
        findall(Pos, contains_paper(Grid, 0, Pos), OriginalPaper),
        findall(Pos, contains_paper(Grid, Step, Pos), FinalPaper),
        length(OriginalPaper, OriginalCount),
        length(FinalPaper, FinalCount),
        PaperRemoved is OriginalCount - FinalCount.
    
    main:-
        Filename = 'inputs/day04.txt',
        read_file_to_strings(Filename, Grid),
        paper_removed(Grid, 1, Result1),
        write("Part 1: "), write(Result1), nl,
        paper_removed(Grid, 100, Result2),
        write("Part 2: "), write(Result2), nl,
        halt.
    
    :- initialization(main).