r/adventofcode • • Dec 04 '25

SOLUTION MEGATHREAD -❄️- 2025 Day 4 Solutions -❄️-

THE USUAL REMINDERS


NEWS


AoC Community Fun 2025: Red(dit) One

  • Submissions megathread is now unlocked!
  • 13 DAYS remaining until the submissions deadline on December 17 at 18:00 EST!

Featured Subreddits: /r/trains and /r/TrainPorn (it's SFW, trust me)

"One thing about trains… it doesn’t matter where they’re going; what matters is deciding to get on."
— The Conductor, The Polar Express (2004)

Model trains go choo choo, right? Today is Advent of Playing With Your Toys in a nutshell! Here's some ideas for your inspiration:

  • Play with your toys!
  • Pick your favorite game and incorporate it into today's code, Visualization, etc.
  • Bonus points if your favorite game has trains in it (cough cough Factorio and Minecraft cough)
  • Oblig: "Choo choo, mother******!" — motivational message from ADA, Satisfactory /r/satisfactorygame
  • Additional bonus points if you can make it run DOOM
  • Use the oldest technology you have available to you. The older the toy, the better we like it!

Request from the mods: When you include an entry alongside your solution, please label it with [Red(dit) One] so we can find it easily!


--- Day 4: Printing Department ---


Post your code solution in this megathread.

25 Upvotes

761 comments sorted by

6

u/voidhawk42 Dec 04 '25 edited Dec 05 '25

[LANGUAGE: Dyalog APL]

p←'@'=↑⊃⎕nget'04.txt'1
f←{⍵∧{4<+/,⍵}⌺3 3⊢⍵}
+/,p-f⊢p ⍝ part 1
+/,p-f⍣≡p ⍝ part 2

Video walkthrough

2

u/daggerdragon Dec 04 '25

Ah, I was wondering if we'd see you and your Alien Programming Language again this year. Welcome back :D

→ More replies (2)

5

u/pitr Dec 04 '25

FYI ⊢⌺ is significantly faster than {}⌺. Using 4<+/⍣2⊢⌺3 3 reduces my total runtime from 5s to 1s

→ More replies (2)

4

u/Salusa Dec 04 '25

[LANGUAGE: MUMPS]

[Red(dit) One] (MUMPS is a language from 1966, that's a pretty old toy/tool :-D)

day4(lines)
    new data,idx,count,x,y,tmp,xoff,yoff
    d parse(.lines,.data)
    set idx="data",count=0
    for  set idx=$query(@idx) quit:idx=""  do
    . s x=$qsubscript(idx,1),y=$qsubscript(idx,2),tmp=0
    . for xoff=-1:1:1 for yoff=-1:1:1 do
    . . s:(xoff'=0)!(yoff'=0) tmp=$get(data(x+xoff,y+yoff),0)+tmp
    . s:tmp<4 count=count+1
    . w:(^debug)&(tmp<4) idx,"=",@idx,"->",tmp,!
    write "Day4: ",count,!
    quit
part2(lines)
    new data,idx,count,x,y,tmp,xoff,yoff,diff
    d parse(.lines,.data)
    set diff=1,count=0
    for  quit:diff=0  do
    . set idx="data",diff=0
    . for  set idx=$query(@idx) quit:idx=""  do
    . . w:^debug "Loop",!
    . . s x=$qsubscript(idx,1),y=$qsubscript(idx,2),tmp=0
    . . for xoff=-1:1:1 for yoff=-1:1:1 do
    . . . s:(xoff'=0)!(yoff'=0) tmp=$get(data(x+xoff,y+yoff),0)+tmp
    . . s:tmp<4 diff=diff+1,count=count+1
    . . kill:tmp<4 u/idx
    write "Day4: ",count,!
    quit
parse(lines,grid)
    new y,x
    set y=""
    for  set y=$order(lines(y)) quit:y=""  do
    . for x=1:1:$length(lines(y)) set:$extract(lines(y),x)="@" grid(x,y)="1"

1

u/daggerdragon Dec 04 '25

[Red(dit) One] (MUMPS is a language from 1966, that's a pretty old toy/tool :-D)

Yes, yes, it is indeed.

→ More replies (1)

3

u/Mahedros Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Ruby]

Link to Code

1

u/daggerdragon Dec 04 '25 edited Dec 04 '25

Comment temporarily removed.

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment. edit: 👍

→ More replies (1)
→ More replies (1)

3

u/Hefty_Repair_9175 Dec 04 '25 edited Dec 05 '25

[LANGUAGE: Matlab]

Quite a nice one today

Github link

1

u/daggerdragon Dec 04 '25 edited Dec 05 '25

Comment temporarily removed.

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment. edit: 👍

→ More replies (3)

3

u/Stano95 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Haskell]

Solution is on github

This is like Conway's game of life but things can only die. Game of death if you like.

Anyway for part 1

  • I read the grid into a Map Coord Char, only keeping hold of the cells with paper rolls in them
  • I iterate though the keys in the Map, for each key find all the neighbours from querying the Map and check if there are fewer than 4 and just take a count

For Part 2

  • I created a step function which would modify my grid getting rid of the paper rolls we can in each step and also it returns how many things I've removed in each step
  • I use unfoldr on this and the answer is just that last element in the list (this is actually the first time I've used unfoldr as well!)

Overall I'm pretty sure I didn't need a Map, a Set would have been just fine since I don't actually ever care about the values in my Map lol. Also there's surely a more efficient way to go through the grid than what I have. But this all works well enough!

EDIT: remove markdown chars

2

u/daggerdragon Dec 04 '25 edited Dec 05 '25

Psst: we can see your Markdown. edit: 👍

2

u/JarroVGIT Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Rust]

Github

Very inefficient solution using a HashMap to store the locations of the rolls (should have been a HashSet, didn't need the values), than simply iterate over the keys and check all directions.

I guess it spends quite some time hashing, so I think it would be MUCH faster if I just used a Vec<Vec<bool>>.

Day 04
------
Part 1: (2.2ms @ 180 samples)
Part 2: (38.1ms @ 25 samples)

EDIT: A solution with Vec<Vec<bool>> runs much faster as expected:

Day 04
------
Part 1: 1564 (57.1µs @ 4214 samples)
Part 2: 9401 (2.5ms @ 365 samples)

1

u/daggerdragon Dec 04 '25 edited Dec 04 '25

Psst: we can see the Markdown on your link. edit: 👍

2

u/MeNoGoodReddit Dec 04 '25 edited Dec 04 '25

[LANGUAGE: JavaScript]

Another browser-dev-tools-console script.
Code on https://topaz.github.io/paste/

1

u/daggerdragon Dec 04 '25 edited Dec 04 '25

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code. edit: 👍

2

u/JamesVagabond Dec 04 '25 edited Dec 04 '25

[Language: Python]

Here's what I came up with.

Not sure what can be improved without ditching the standard approach to grid navigation. I hear that complex numbers can offer a far neater way of handling grids, but I can't really say that the usual way of doing things has inconvenienced me all that much in this case.

1

u/daggerdragon Dec 04 '25 edited Dec 04 '25

Comment temporarily removed.

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment. edit: 👍

→ More replies (1)

2

u/MrJakobsen Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Python]

Set based solution, I do not know if it matters for speed, but seemed obvious

https://github.com/BoJakobsen/AoC/blob/main/AoC2025/src/puz_04.py

1

u/daggerdragon Dec 04 '25 edited Dec 07 '25

Comment temporarily removed.

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment. edit: 👍

2

u/Smylers Dec 04 '25

[LANGUAGE: Vim keystrokes] This does fit on a punchcard, though only just. Load your input — or, to see what's going on better, the sample input — and type:

:se nows⟨Enter⟩:%s/@/0/g|%s/.*/.&.⟨Enter⟩yypVr.yy{P
PxC⟨Ctrl+R⟩=strlen('⟨Ctrl+R⟩-')⟨Enter⟩⟨Enter⟩⟨Esc⟩
/\.\d/e⟨Enter⟩qaqqa⟨Ctrl+V⟩⟨Ctrl+A⟩n@aq@a{/\d\.⟨Enter⟩@aqb{kyiw⟨Ctrl+A⟩
/\v(\._.{⟨Ctrl+R⟩0})@<=\d⟨Enter⟩:norm@a⟨Enter⟩{/\v\d(_.{⟨Ctrl+R⟩0}\.)@=⟨Enter⟩
:norm@a⟨Enter⟩q@b@b{dk:%s/[^5-8]//g⟨Enter⟩VgggJg⟨Ctrl+G⟩

Each roll of paper is going to count its neighbours, so turn each @ into a zero. I accidentally wrote regexp for counting exactly the wrong thing (non-roll-of-paper neighbouring spaces, rather than ones with paper), so to make it work add a border of extra .s all the way round: the :%s/// puts them at the left and right and copying a line then using Vr. to make it all dotty is used for the bottom and top.

At some point we're also going to need to know the number of positions in a row, so P a second copy and replace it with its length and an empty line. Actually, x it first to get one less than its length.

Then use 8 patterns, to find in turn instances of a roll of paper with non-paper in a particular direction. These do left and right:

/\.\d/e
/\d\./

The trailing /e means the cursor goes to the end of the pattern, which ensures it's on the roll of paper, not the non-paper to its left. When a roll is found use ⟨Ctrl+V⟩⟨Ctrl+A⟩ to increase is non-paper-neighbour count by 1. Record this in @a, with n to make it find the next roll of paper with non-paper in the current direction and repeat itself with a nested @a. The direction-specific / command is before the loop, so the macro itself is identical for each direction. To make it stop after all rolls of paper have been found for a direction, we need searches not to wrap round from the end to the beginning, so there's :set nowrapsearch at the start.

The patterns for non-paper diagonally to the top-right and bottom-left are something like:

/\v(\._.{11})@<=\d
/\v\d(_.{11}\.)@=

where 11 is the one-less-than-line-length determined earlier, which is yanked and then inserted into the pattern with ⟨Ctrl+R⟩0. In Vim patterns /_./ is like /./ but also matches line-breaks.

Patterns for directly-above and directly-below are the same but with 11 replaced by 12. And top-left and bottom-right need 13. So record doing those searches and running @a after each in @b. In order for the ‘no more found’ error only to exit the inner @a loop not the outer @b loop, invoke the inner one with :norm @a. Increase the line-length number with ⟨Ctrl+A⟩ each time after yanking it, ready for the next time through @b.

Having annotated each roll with its count of of non-paper neighbours, remove the line-length count from the top, remove everything that isn't a digit 5–8, join the remaining rolls of paper onto a single line, and then the part 1 answer is the number of columns in that line, shown with g⟨Ctrl+G⟩.

If I'd had the foresight to save the line-length-counting keystroke to another macro, we could've just used that at the end to put the answer in the window. But I didn't.

2

u/daggerdragon Dec 04 '25 edited Dec 04 '25

It's December in upstate NY, I shouldn't have to go fishing for /u/Smylers posts in the spam filter pond every day -_-

There's got to be a way to set you as an approved poster somehow. brb going hunting (without a license) through subreddit settings... edit: there, you are now an approved user for /r/adventofcode -_-

→ More replies (1)

2

u/stogas Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Go]

Github

How fast are y'alls solutions? I just bruteforced Day 4 and it still ran in 8ms, and I wonder if it's just Go optimizing the hell out of my code, or if I should start to target sub-1ms, like many of the Rust people here seem to achieve...

1

u/daggerdragon Dec 04 '25

Do not share your puzzle input which also means do not commit puzzle inputs to your repo without a .gitignore or the like. Do not share the puzzle text either.

I see full plaintext puzzle inputs in your public repo e.g.:

https://github.com/Stogas/aoc2025-go/blob/main/day4/input.txt

Please remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. This means from all prior years too!

→ More replies (1)
→ More replies (5)

2

u/mestar12345 Dec 04 '25 edited Dec 06 '25

[Language: F#]

let neighbors  = 
    [ -1,-1; 0,-1; 1,-1
      -1, 0;       1, 0
      -1, 1; 0, 1; 1, 1 ]

let data = File.ReadAllLines filename 

let generateZoneOfInfluence x y zone = 
    neighbors
        |> List.map (fun (a,b) -> x+a, y+b)
        |> List.fold (fun m (x,y) ->
            let newval = 
                if m |> Map.containsKey (x,y) then
                     1 + m[x,y]
                else 1
            m |> Map.add (x,y) newval 
        ) zone

1

u/daggerdragon Dec 04 '25

Comment temporarily removed.

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment.

2

u/fallenmink Dec 04 '25

[Language: Python]

Full code with setup here.

Only part 2 is included as I wrote it over top of part 1. But the logic is the same, just with more iterations/checks.

def process_row(row, computed):
    global rolls
    changed = True
    memo = sum(row)
    while changed == True:
        indices = []
        for index_inner, element in enumerate(row):
            total = -1
            if element == 0:
                continue
            if index_inner != 0:
                total += computed[index_inner-1]
            if index_inner != len(row) - 1:
                total += computed[index_inner+1]
            total += computed[index_inner]
            if total < 4:
                rolls += 1   
                indices += [index_inner]
        for i in indices:
            row[i] = 0
        if sum(row) == memo:
            changed = False    
        memo = sum(row)
    return row

def process_rows(rows):
    global rolls
    changed = False
    for index, row in enumerate(rows):
        if index == 0 or index == (len(rows)-1):
            continue
        memo = sum(row)
        computed = [x+y for x,y in zip(row, rows[index+1])]
        computed = [x+y for x,y in zip(computed, rows[index-1])]  
        row = process_row(row, computed)
        rows[index] = row
        if memo != sum(row):
            changed = True
    return changed


while process_rows(rows):
    ...

print(rolls)

1

u/daggerdragon Dec 04 '25 edited Dec 04 '25

FYI: your account is (shadow?)banned so your posts won't be visible (to regular users) on any subreddit. There's nothing we can do about that; you'll have to take it up with Reddit: https://www.reddit.com/appeals

2

u/Lindi-CSO Dec 04 '25

[LANGUAGE: C#]

Today felt like a really gentle introduction to grid based puzzles. Thanks for easing us in.

Day-04 solution for Part 1 and Part 2

→ More replies (3)

2

u/VictoriousEgret Dec 04 '25 edited Dec 04 '25

[LANGUAGE: R]

Here was my solution. I'm now going through it and trying to update it for performance (in this form it takes about 4-5 minutes to run part 2]. I've seen some excellent solutions using matrices that are inspired.

https://github.com/seanlacey/advent2025/blob/main/day4/day4.R

1

u/daggerdragon Dec 04 '25 edited Dec 04 '25

Comment temporarily removed.

This is the second time I've instructed you to put your oversized code in an external link. The onus is on you to calculate whether your code is too large for the megathreads before you post it.

Put your oversized code in an external link and I will re-approve this post. edit: 👍


Before you post in a Solution Megathread again, read the rules in our community wiki > Solution Megathreads > rules for posting in Solution Megathreads.

→ More replies (1)

2

u/totoka282 Dec 05 '25

5

u/daggerdragon Dec 05 '25

FYI: in your .gitignore I see that you're manually excluding each day's input file like this:

/Day1/data.txt
/Day2/Data.txt
/Day1/data.txt
/Day3/data.txt
/Day4/data.txt

There's a duplicate Day1 line in there, and manually updating the .gitignore isn't future-proof, which means you're at risk of accidentally exposing one of your puzzle inputs if you forget to update the .gitignore before committing.

I recommend you use a wildcard and condense all these lines down to something like:

**/[dD]ata.txt
→ More replies (1)

2

u/stevie-o-read-it Dec 05 '25

[Language: Intcode]

I wonder if I'll be able to do everything in Intcode this year. Probably not, but I'm 4-for-4 so far, which I think is pretty cool. Nothing on the level of my 2024 Day 17 solver, though.

Like days 1 and 2, the unmodified compiled code accepts the input file in ASCII and produces the answer for both parts.

Produces the correct answers for both the example and my puzzle input.

  • The example took 73ms and executed 36623 opcodes.
  • My puzzle input took about 1.5s and executed 7352996 opcodes.
  • For added fun, try feeding it the second image from the example (the one with the 'x's)

Compiled Intcode file

Original assembly

Compiler

(NOTE: The compiler got a new feature today to make it easier to code this)

AOC 2025 Day 4 Intcode Solver

Input is in ASCII (UTF-32/UCS-4), output is in ASCII (UTF-32/UCS-4).

Features:

  • Solves both parts
  • No built-in limits on input size -- whatever your IntcodeVM can handle Note that intcode isn't very efficient, so it needs a LOT of memory.

All CRs ('\r' aka 13 or 0x0D) in input are completely ignored.

Since the input size is indeterminate, EOF must be reported via any of these means:

  • A blank line
  • 0x00 (ASCII NUL)
  • 0x1A (Ctrl-Z, ASCII SB; MS-DOS or CPM end-of-file indicator)
  • 0x04 (Ctrl-D, ASCII EOT; Default EOF replacement character on *nix-style TTY drivers)
  • a negative number (EOF constant returned by fgetc, getc, or getchar; see stdio.h)

EXECUTION OPTIONS BLOCK - Memory addresses 3-5

[3]: debug_mode. Default 0. If 1, output the number of rolls removed after each pass. [4]: raw_output. Default 0. 0 = ASCII output for integers. 1 = Output all integers as raw integer values. [5]: use_dle: Default 1. 1 = Prefix all raw integer outputs with a a DLE (0x10) character. This flag will normally have no effect unless RawOutput is enabled.

1

u/daggerdragon Dec 06 '25

I wonder if I'll be able to do everything in Intcode this year. Probably not, but I'm 4-for-4 so far,

You can do it!

Or if you prefer:

Do it. Do it do it do it!

→ More replies (1)

34

u/4HbQ Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Python + NumPy] 5 lines.

Puzzles where we have to use the state of adjacent cells are easy to solve using a NumPy array and convolution. First we convolve using a kernel that counts the number of adjacent cells that are "on" (in this case, containing a roll of paper). Based on those counts, we disable active cells that have a count less than 5 (not 4, because we also count the cell itself):

count = convolve2d(grid, np.ones((3,3)), mode='same')
grid ^= grid & (count < 5)

This is equivalent to just keeping (using the logical &) the cells that have more than 4 active adjacent cells:

grid &= convolve2d(grid, np.ones((3,3)), mode='same') > 4

Update: for those that don't like to rely on NumPy for the heavy lifting, we can do almost the same in 7 lines of pure Python code. We define our "kernel" as the set of cells adjacent to (x,y):

{(i,j) for i in [x-1, x, x+1] for j in [y-1, y, y+1]}

and use it to update the grid:

grid &= {(x,y) for x,y in grid if len({...} & grid) > 4}

7

u/GrumDum Dec 04 '25

Another day of AOC, another learning moment from /u/4HbQ.

It’s beginning to look a lot like Christmas!

Ps. Thanks for sharing, as always.

→ More replies (4)
→ More replies (8)

13

u/light_switchy Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Dyalog APL]

Everyone loves stencil code!

Part 1:

+⌿,{⍵[2;2]∧4≥+⌿,⍵}⌺3 3⊢↑'@'=⊃⎕NGET '4.txt' 1

Part 2:

(⊣-⍥(+⌿,)(⊣≠{⍵[2;2]∧4≥+⌿,⍵}⌺3 3)⍣≡)↑'@'=⊃⎕NGET '4.txt' 1

2

u/Asleeper135 Dec 04 '25

[LANGUAGE: Rust]

fn count_adjacent_rolls(grid: &[Vec<u8>], row: usize, col: usize) -> AnswerType {
    let row_min = row.saturating_sub(1);
    let row_max = (row + 1).min(grid.len() - 1);
    let col_min = col.saturating_sub(1);
    let col_max = (col + 1).min(grid[0].len() - 1);

    (row_min..=row_max)
        .flat_map(|r| (col_min..=col_max).map(move |c| grid[r][c] == b'@'))
        .filter(|b| *b)
        .count()
        - 1
}

fn parse(input: &str) -> Vec<Vec<u8>> {
    input
        .lines()
        .map(|l| l.as_bytes().to_owned())
        .collect_vec()
}

fn part1(_input: &str) -> AnswerType {
    let grid = parse(_input);
    let mut count = 0;

    for row in 0..grid.len() {
        for col in 0..grid[0].len() {
            if grid[row][col] == b'@' && count_adjacent_rolls(&grid, row, col) < 4 {
                count += 1;
            }
        }
    }

    count
}

fn part2(_input: &str) -> AnswerType {
    let mut grid = parse(_input);
    let mut count = 0;
    let mut removed = false;

    loop {
        for row in 0..grid.len() {
            for col in 0..grid[0].len() {
                if grid[row][col] == b'@'
                    && count_adjacent_rolls(&grid, row, col) < 4 {
                        count += 1;
                        grid[row][col] = b'.';
                        removed = true;
                    }
            }
        }

        if !removed { break } else { removed = false }
    }

    count
}

3

u/Skaarj Dec 04 '25

3

u/daggerdragon Dec 04 '25

Good, good, you've fallen for /u/topaz2078's trap of ~sneakily making people learn new things~ <3

11

u/Andreasnl Dec 04 '25

[LANGUAGE: Uiua]

Today's task was perfect for an array language such as Uiua.

F  ← ×⊸⬚0⧈(≤4⧻⊚)[3_3 1_1 1_1]
P₁ ← ⧻⊚ F
P₂ ← ⧻⊚ -⊸⍥(-⊸F)∞
⊃P₁ P₂ =@@ ⊜∘⊸≠@\n &fras"4.txt"

Link

→ More replies (4)

10

u/zniperr Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Python]

Saves the paper coordinates as a set to avoid having to deal with edge conditions on a grid.

import sys

def inaccessible(papers):
    for x, y in papers:
        neighbors = {(x - 1, y - 1), (x, y - 1), (x + 1, y - 1),
                     (x - 1, y    ),             (x + 1, y    ),
                     (x - 1, y + 1), (x, y + 1), (x + 1, y + 1)}
        if len(papers & neighbors) >= 4:
            yield x, y

def removable(papers):
    original = len(papers)
    new = set(inaccessible(papers))
    while len(new) != len(papers):
        papers = new
        new = set(inaccessible(papers))
    return original - len(papers)

papers = {(x, y) for y, line in enumerate(sys.stdin)
                 for x, char in enumerate(line)
                 if char == '@'}
print(len(papers) - sum(1 for _ in inaccessible(papers)))
print(removable(papers))
→ More replies (2)

9

u/seligman99 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Python]

github

A grid! It's feeling like AoC in here now.

Edit: and a quick animation of the paper being cleared for part 2

6

u/psm_pm Dec 04 '25

As soon as I saw the grid I was dreading having to simulate pushing :P

→ More replies (1)

8

u/JustinHuPrime Dec 04 '25

[LANGUAGE: x86_64 assembly]

Part 1 involved a bunch of fiddling around to allocate a large enough buffer (I decline succumb to the C programmer's disease and pre-allocate buffers), and then a straightforward read and scan (with an oversized buffer to avoid bounds checks) gave the answer.

Part 2 involved allocating a second buffer and swapping between them, similar to how double-buffering works. The same scan and some additional logic and I got the answer.

Part 1 runs in 1 millisecond, and part 2 in 4 milliseconds. Part 1 is 9,280 bytes and part 2 is 9,360 bytes as a linked executable.

→ More replies (2)

7

u/jonathan_paulson Dec 04 '25 edited Dec 04 '25

[Language: Python]. My times: 00:01:40 / 00:02:30. Video of me solving.

Brute force for both parts today; we can just straightforwardly do what we're told. I counted a square as a neighbor to itself, which changes the condition from less-than-4 to less-than-5. Part 2 just requires a loop around the same logic as part 1.

→ More replies (2)

7

u/SheepiCagio Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Excel]

Input in A1:A139

 =LET(
map;TOCOL(DROP(REDUCE(0;A1:A139;LAMBDA(a;v;VSTACK(a;MID(v;SEQUENCE(LEN(v));1))));1));
addr;FILTER(TOCOL(SEQUENCE(LEN(A1);;10000;10000)+SEQUENCE(;LEN(A1)));map="@");
remainingaddr;FILTER(addr;MAP(addr;LAMBDA(a;--(SUM(--(ISNUMBER(
XMATCH(a+{10000;-10000;1;-1;10001;-10001;-9999;9999};addr;0))))>=4))));
rows(addr)-rows(remainingaddr))

Made part 2 in a single cell formula as well. Although it takes a bit to calculate:

=LET(   map;TOCOL(DROP(REDUCE(0;A1:A139;LAMBDA(a;v;VSTACK(a;MID(v;SEQUENCE(LEN(v));1))));1));
addr;FILTER(TOCOL(SEQUENCE(LEN(A1);;10000;10000)+SEQUENCE(;LEN(A1)));map="@");
remainingaddr;REDUCE(addr;SEQUENCE(55);LAMBDA(acc;v;
FILTER(acc;MAP(acc;LAMBDA(a;--(SUM(--(ISNUMBER(
XMATCH(a+{10000;-10000;1;-1;10001;-10001;-9999;9999};acc;0))))>=4))))));
ROWS(addr)-ROWS(remaining))

6

u/maneatingape Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Rust]

Solution

Simple brute force for now..

Solves both parts together. Part two uses a queue to track points that need to be removed. 244µs total.

→ More replies (3)

6

u/tymscar Dec 04 '25

[LANGUAGE: Gleam]

Loved today's puzzle. Couldn't help myself and got it visualised in the end too!

For part 1, the hardest problem was figuring out how to think through this in a functional gleam way. Ended up using dicts with positions as keys. I would then go through them all and filter those with fewer than 4 neighbours. Because the dictionary access would return an option, the filtering was very clean.

For part 2, because I don't have for loops, I created a function that recursively filters out the opposite of part 1, positions that had 4 or more neighbours. And I would recurse it until the new grid was identical in size to the old one (nothing changed). Then I would subtract the last grid size from the initial one and that was the answer.

Part1 and Part2

6

u/hrunt Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Python]

code

Ahh, the age old AoC conundrum. Do I represent the 2D grid as a 2D array or a dictionary of points? Is Part 2 going to be "extend to infinity" or "do something with the grid until it stops"?

For part 1, I originally just kept a 2D array (list of strings), with an edge border of no rolls to not have to worry about array out of bounds. When I saw Part 2, I converted that code to just maintain a dictionary of points with counts to make iterating the moveable rolls easier.

5

u/lluque8 Dec 04 '25

[Language: Scala]

github

Surprisingly easy one today :)

→ More replies (1)

5

u/r_so9 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: F#] paste

Direct simulation trying all spaces on each iteration (n2 ), with no optimizations to remove in-place. The grid is represented by a set of (row, column) pairs with the coordinates of the rolls.

Part 2 is a Seq.unfold (of course). Here's the full code minus helpers and parsing:

let isAccessible grid pos =
    pos
    |> neighbors
    |> Seq.filter (fun neighbor -> Set.contains neighbor grid)
    |> Seq.length
    |> fun ns -> ns < 4

let part1 = initialGrid |> Set.filter (isAccessible initialGrid) |> Set.count

let removeAccessible grid =
    match Set.filter (isAccessible grid) grid with
    | empty when Set.isEmpty empty -> None
    | toRemove -> Some(Set.count toRemove, Set.difference grid toRemove)

let part2 = Seq.unfold removeAccessible initialGrid |> Seq.sum
→ More replies (2)

5

u/mstksg Dec 04 '25

[LANGUAGE: Haskell] https://github.com/mstksg/advent-of-code/wiki/Reflections-2025#day-4

For these I have a handy utility function to parse an ascii map into a set of points:

data V2 a = V2 a a

parseAsciiSet :: (Char -> Bool) -> String -> Set (V2 Int)

and also a handy function that gets all eight neighbors of a point:

fullNeighbsSet :: V2 Int -> Set (V2 Int)

They're actually fun to implement, exercise left to reader :)

Anyway once you have those, you can write a function of all reachable rolls:

reachable :: Set (V2 Int) -> Set (V2 Int)
reachable pts = flip S.filter pts \pt ->
  S.size (fullNeighbsSet pt `S.intersection` pts) < 4

And so we have:

part1 :: Set (V2 Int) -> Int
part1 = S.size . reachable

part2 :: Set (V2 Int) -> Int
part2 = S.size . fold . takeWhile (not . S.null) . unfoldr (Just . go)
  where
    go pts = (removed, pts `S.difference` removed)
      where
        removed = reachable pts
→ More replies (1)

5

u/ziadam Dec 04 '25 edited Dec 07 '25

[LANGUAGE: Google Sheets]

Part 1 & 2 (expects input in A1)

=ARRAYFORMULA(LET(
  i, SUBSTITUTE(A1, CHAR(10), ),
  l, LEN(i),
  w, l^0.5,
  k, ROW(1:8)^0,
  s, SEQUENCE(l),
  m, N(MID(i, s, 1)="@"),
  x, s + {-w-1, -w, -w+1, -1, 1, w-1, w, w+1},
  h, TOCOL(IF((x<1) + (x>l) + (ABS(MOD(x-1, w) - MOD(s-1, w)) > 1), l+1, x)), 
  {
    SUM(m * (MMULT(WRAPROWS(CHOOSEROWS({m;0}, h), 8), k)<4));
    SUM(m) - SUM(REDUCE(m, SEQUENCE(75), LAMBDA(a, _,
       IF(MMULT(WRAPROWS(CHOOSEROWS({a; 0}, h), 8), k) < 4, 0, a)
    )))
  }
))

4

u/K722003 Dec 04 '25

[Language: Python] Time: 00:00:50 / 00:01:40

Directly brute forced for part 1, reused the same code and used it for part 2.

P1: If its a '@' check all 8 neighbours and increment if count less than 4

P2: Call P1 function but mutate the input each time with '.' and keep a cumulative sum of taken items. Repeat until returned value and input are the same.

→ More replies (2)

4

u/morgoth1145 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Python] code video Times: 00:02:02 / 00:03:12

Here's our first grid problem! I have a nice grid library to help with these, but I dallied an annoying amount due to a lack of confidence in remembering the API despite literally reviewing it a little before the problem dropped. So that's embarrassing, but it is what it is.

Edit: Rewritten, optimized code

A simple deduplication refactor would have sufficed here but part 2 was annoyingly slow so I decided to fully rewrite and optimize the solution instead. It runs much faster when:

  1. Pre-calculating neighbor counts and updating the neighbor counts when TP is removed
  2. Tracking which cells are affected by the last iteration and only checking those instead of all cells

Now part 2 takes ~0.06 seconds instead ~0.6 seconds, a nice 10x speedup!

4

u/PendragonDaGreat Dec 04 '25 edited Dec 04 '25

[LANGUAGE: C# CSharp]

Both parts: https://github.com/Bpendragon/AdventOfCodeCSharp/blob/5f8579/AdventOfCode/Solutions/Year2025/Day04-Solution.cs

First time in the year to use and abuse my Coordinate2D class and the related GenerateMap function that I first wrote back in like 2020. It just takes the input and creates a Hastable/Dictionary of each element in the input, by default discarding . as empty space. Then it's just a matter of using the Neighbors function on my Coordinate2D class to find those with less than 4 neighbors and iteratively remove them.

Felt real good to get both parts in under 13 minutes.

edit: LINQ to make part 1 a one liner and part 2 as close as can be: https://github.com/Bpendragon/AdventOfCodeCSharp/blob/fb964/AdventOfCode/Solutions/Year2025/Day04-Solution.cs

→ More replies (2)

4

u/Boojum Dec 04 '25

[LANGUAGE: Python]

Ah, a cellular-automata-like puzzle! This was fun. Using sets of coordinates to represent the rolls on the grid remains the way to go here. Change the if not c: break to just break for the Part 1 solution.

import fileinput

g = { ( x, y )
      for y, r in enumerate( fileinput.input() )
      for x, c in enumerate( r.strip( '\n' ) )
      if c == '@' }

t = 0
while True:
    c = [ ( x, y )
          for x, y in g
          if sum( ( x + nx, y + ny ) in g
                  for nx in ( -1, 0, 1 )
                  for ny in ( -1, 0, 1 ) ) <= 4 ]
    t += len( c )
    if not c: break
    g = g.difference( c )
print( t )

4

u/sim642 Dec 04 '25

[LANGUAGE: Scala]

On GitHub.

This was very straightforward with a Grid and Pos library from all the previous years. The common functionality of both parts is the function which returns a list of all accessible roll positions. Part 1 is just the length of that list.

In part 2 I remove the currently accessible rolls and repeat recursively, summing up their counts, pretty much exactly what the example also shows.

→ More replies (1)

4

u/KyleGBC Dec 04 '25

[LANGUAGE: Rust]

Someday I will either find or create a handful of re-usable grid types/functions for AoC and I'll be able to solve this puzzle in like two lines. Oh well, at least itertools is a friend.

Solution

4

u/TheDingusJr Dec 04 '25 edited Dec 04 '25

[Language: Python]

My solution using convolutions - first convert the grid to a 2D array of booleans, apply the convolution to get the number of occupied neighbors at each position, and then do some booleaning to get the next iteration of the grid after removal. Solves part 2 in about 30ms, and you can add a log of the difference at each iteration to get part 1.

Edit: scipy.ndimage.convolve is faster than scipy.signal.convolve2d. Getting about 10ms now

def remove_rolls(grid):
    new_grid = grid & ((convolve(grid.astype(int), np.ones((3, 3)), mode="constant") - 1)>=4)
    return new_grid if np.array_equal(new_grid, grid) else remove_rolls(new_grid)

grid = np.array([[c == "@" for c in row] for row in input.splitlines()])
print(grid.sum() - remove_rolls(grid).sum())

4

u/Pyr0Byt3 Dec 04 '25

[LANGUAGE: Go] [LANGUAGE: Golang]

https://github.com/mnml/aoc/blob/main/2025/04/1.go

Love doing these types of problems by using a map of points for the grid.

4

u/michelkraemer Dec 04 '25

[LANGUAGE: Rust]

Solution

Runs in 110µs (both parts).

When counting the neighbors of each roll in part 1, put all rolls that can be accessed into a queue. In part 2, for each roll in the queue, remove them and decrease the count of all their neighbors. If the count of a neighbor falls below the limit, add it to the queue too. Only do this if the count was exactly 4 and will now become 3. This avoids adding a roll multiple times to the queue. Repeat this until the queue is empty.

4

u/melochupan Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Crystal]

This one took ~20 lines and can't be inlined. At first I tried to use Array#[]? to count the neighbors (it returns nil on out-of-bounds), but was tripped by the feature that array[-1] isn't out of bounds: it refers to the last item in the array. It took me some time to squash that bug.

paste

4

u/0rac1e Dec 04 '25 edited Dec 04 '25

[Language: J]

m =. '@' = 'm' freads 'input'
r =. 0 0 -.~ ,/ ,"0/~ i: 1
echo +/ ,     r&(]     * 4 > [: +/ |.!.0)    m
echo +/ , m - r&(] - ] * 4 > [: +/ |.!.0)^:_ m

and obligatory generalization

F =: (,/ ,"0/~ i: 1)&(] - ] * 4 >: [: +/ |.!.0)
echo m&{{ +/ , x - F^:y x }}"+ 1 _

EDIT: It just occurred to me that if I swap the comparison (from > to ≤) then I don't need to do that extra subtraction. My original solution could have been written as

echo +/ ,     r&(] * 4 >  [: +/ |.!.0)    m
echo +/ , m - r&(] * 4 <: [: +/ |.!.0)^:_ m

Which looks a little nicer

4

u/Chungus1234 Dec 04 '25

[LANGUAGE: Elixir]

No easy way to do 2D arrays in elixir so just used a map for the grid. Curious to see what others did.

https://github.com/moore-andrew05/AoC2025-elixir/blob/main/lib/day04.ex

→ More replies (1)

4

u/Porges Dec 04 '25 edited Dec 04 '25

[LANGUAGE: APL]. It's nice here!

Part 1:

input ← '@'= ⊃⎕NGET 'input4.txt' 2
movable ← (5> {+/,⍵} ⌺ 3 3) ∧ ⊢
⎕ ← +/, movable input
  • load the data as a matrix with 1 where '@' was
  • define a 'movable' function: movable items are those with less than 5 neighbours (including themselves)
  • we identify these and calculate the sum

Part 2:

input ← '@'= ⊃⎕NGET 'input4.txt' 2
movable ← (5> {+/,⍵} ⌺ 3 3) ∧ ⊢
result ← 0 @ movable ⍣≡ input
⎕ ← +/, input - result
  • using our 'movable' function we can create a function 0 @ movable which updates a given matrix to remove any movable ones
  • we then simply iterate this to a fixed point (⍣≡) on the input
  • output the sum of the difference between start and end states
→ More replies (1)

4

u/Radiadorineitor Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Dyalog APL]

p←'@'=↑⊃⎕NGET'4.txt'1
F←⊢∧⊢∘(5>+/⍤,)⌺3 3∘⊢
+/,F p ⍝ Part 1
+/,p-(⊢-F)⍣≡p ⍝ Part 2
→ More replies (2)

4

u/azzal07 Dec 04 '25

[LANGUAGE: awk] Mawk gets weird with negative substr() offsets... which is fair, to be honest.

END{for(;b-->-!B;$++B=A){for(s=i=2;c=substr(S,i++,1);s=s c)
1~c?3:split(substr(g=11S,W<i?i-W:1,3)substr(g,i,3)substr(g,
i+W,3),G,c)>5?0:A+=b=++c;S=s}print$1RS$B}S=1$0S{W=length+1}

4

u/birdnardo Dec 04 '25

[Language: Mathematica]

Convolutions!

map = ArrayPad[
   Characters /@ StringSplit[input, "\n"] /. {"." -> 0, "@" -> 1}, 1];
kernel = {{1, 1, 1}, {1, 100, 1}, {1, 1, 1}};

(*part 1*)
Count[ListConvolve[kernel, map], _?(99 < # < 104 &), {2}]

(*part 2*)
f[{m_, c_}] :=
 Return[{ReplacePart[m, (# + 1) -> 0], c + Length[#]} &@
   Position[ListConvolve[kernel, m], _?(99 < # < 104 &), {2}]]
NestWhile[f, {map, 0}, UnsameQ, 2][[2]]

(*bonus: make a cute 5s video*)
l = NestWhileList[f, {map, 0}, UnsameQ, 2];
lPlots = 
  MatrixPlot[#] & /@ l[[1 ;; Length[l] - 1, 1, 2 ;; 140, 2 ;; 140]];
VideoGenerator[lPlots, 5]

4

u/AldoZeroun Dec 04 '25

[Language: Zig]

github link

Today felt so cathartic. After last year, which I did in GDScript, I got used to having an array of easy to use data types like Vector2i. Because I made the plan to do this year in zig I wrote my own generic vector type so that I could continue to use my same coding patterns (using vector2's as keys into a hashmap makes searching a grid such a breeze). All that work didn't go for nothing!

4

u/SwampThingTom Dec 04 '25

[Language: Swift]

Day 4 GitHub

5+ years of AoC has conditioned me to always store grids in sets or hashmaps. That made today's remarkably easy!

4

u/Most_Print8878 Dec 04 '25

[LANGUAGE: Racket & Io]

Solved this in two different languages as I've been going back and forth between them:

4

u/marcus_cemes Dec 04 '25

[LANGUAGE: Veryl]

This my 4th solution in Veryl, a Hardware Description Language (HDL). Today's problem lends exceptionally well to parallelism (each grid neighbourhood synthesises well into a small cluster of adders), and the problem is sufficiently small to fit into registers, thus each grid reduction (one entire grid traversal) can be effectively computed in a single clock cycle. I'm a little worried that my removal counting would synthesise to an enormous adder tree, I may try to improve this by counting each row over multiple cycles instead (138 extra cycles per grid reduction).

My Rust solution takes 283 µs (5.7 GHz), using a 1-cell padded grid, stored as a contiguous `Vec<bool>` and using unsafe indexing (no bound checks). The hardware version simulates to 19 µs (1 GHz), 99% of which is spent reading the input, one byte at a time, 19191 characters).

https://github.com/MarcusCemes/advent-of-code-2025/blob/main/hardware/04.veryl

→ More replies (2)

4

u/e_blake Dec 04 '25

[LANGUAGE: m4]
[Red(dit) One]

I think an m4 solution counts as playing with my old toys. After all, the m4 language was introduced to the world in 1977 (hey - that's my birth year!), so it's one of the oldest scripting languages still in modern use! (It doesn't hurt that I'm now at 508 stars with m4 solutions...)

For my initial implementation (get the gold star, worry about optimizing later), I just did a brute force loop over every grid index until nothing changed (for my input: 40 loops * 19k points, with 8 lookups per defined point), using my common.m4. I'm sure there are faster ways (tracking which points are defined, instead of wasting time iterating over the sparse areas; maybe attempting to store the grid as a bitmap for parallel processing, ...). So it took 3.7s of execution, with over 1.6m defn, 769k ifdef, 1.4m eval). On the other hand, it might be easy to golf...

m4 -Dfile=day04.input day04.m4

I did have fun using a 1-D array (yes, I know all you python coders with complex numbers like manipulating 2-D arrays with a single-entity coordinate; but m4 is too old to have built-in complex numbers). I used an initial offset to avoid negative indices, and exploited the newlines in the input for an automatic boundary. Doing my neighbor count is merely a matter of 8 point lookups at 8 relative addresses:

define(`neighbors', `len(defn(`g'eval($1-'y`-1))defn(`g'eval($1-'y`))defn(
  `g'eval($1-'y`+1))defn(`g'decr($1))defn(`g'incr($1))defn(`g'eval($1+'y`-1
  ))defn(`g'eval($1+'y`))defn(`g'eval($1+'y`+1)))')
→ More replies (2)

4

u/SpudPanda Dec 04 '25

[LANGUAGE: rust]

I was really tempted to try and figure out a non brute force method. Honestly just updating characters as I went along for part 2 was more than fine. In the real world I'd be pretty happy with these results.

Part 1: 148.208µs
---
Part 2: 1.659709ms

Solution

4

u/kwenti Dec 04 '25

[Language: Python]

Using complex numbers to ease expressing translations, and a `defaultdict` to dispense of out-of-bounds checks, were the tricks of the day.

from collections import defaultdict
d = defaultdict(str)
for y, l in enumerate(open(0).read().split()):
    for x, c in enumerate(l):
        d[x + y * 1j] = c
U = [x + y * 1j for x in (-1, 0, 1) for y in (-1, 0, 1)]
def can_remove(z):
    return d[z] == "@" and (sum(d[z + dz] == "@" for dz in U if dz) < 4)
p1 = sum(can_remove(z) for z in list(d))
p2 = 0
while (r := len([d.pop(z) for z in list(d) if can_remove(z)])) > 0:
    p2 += r
print(p1, p2) 

I tried to assign p1 as the first r computed in the loop, but that was a mistake: the dictionary is mutated by pop() at every iteration of the list comprehension, so more paper rolls are remove than part 1 allows...

→ More replies (2)

4

u/semicolonator Dec 04 '25

[LANGUAGE: Python]

25 Lines

I am using convolution with a kernel that sums up over all neighbors, and then I only select those points on the grid where we sum is less than four.

4

u/RazarTuk Dec 04 '25

[LANGUAGE: Kotlin]

Yeah, I didn't feel like attempting this one in LOLCODE, though I might go back later to try it. Anyway, the most interesting part, as I build up a library to help with future problems/years, was a snazzy little Grid class that you can index with complex numbers

class Grid<T>(val rows: Int, val cols: Int) {
    val grid = Array(rows) { arrayOfNulls<Any?>(cols) }

    operator fun <T> get(i: Complex): T {
        return grid[i.getReal().toInt()][i.getImaginary().toInt()] as T
    }

    operator fun <T> set(i: Complex, v: T) {
        grid[i.getReal().toInt()][i.getImaginary().toInt()] = v
    }
    
    fun <T> getOrDefault(i: Complex, default: T): T {
        try {
            val tmp = grid[i.getReal().toInt()][i.getImaginary().toInt()] as T?
            return tmp ?: default
        } catch (e: IndexOutOfBoundsException) {
            return default
        }
    }
}

4

u/G_de_Volpiano Dec 04 '25

[LANGUAGE: Haskell]

Couldn't paste my solution earlier, so I'm coming with a fully optimised solution now.

Started with an IntSet, but I'd misread the problem and thought the whole area was walled (so outer bounds were not accessible), which obviously gave wrong results.

Overengineered a sliding-window solve as you parse solution on my way to work which work but was obviously unsuitable for part 2.

I rewrote the first solution, and lazily went for redoing the same "prune your IntSet" routine until I got the same IntSet out that I had sent in. Which I checked by using size.

It worked but was darn slow, c. 60ms. That's when I realised that size was O(n) and not O(1) as I thought. Replaced size by ==, and cut the running time in half.

After that, I implemented an actual pruning logic (only neighbours of removed rolls are susceptible to be free, so no need to relook at the others), which once again cut running time in half. Which is when I realised that if I was know only looking for a small subset of the rolls at each pass, then a Vector, despite being less tightly packed than my IntSet, would be more efficient as the lookup table. So time to go back to the good old Data.Vector.Mutable.Unboxed. Which frustratingly brought me just above the 1ms barrier. Replace read and write with their unsafe variants, and voilà, the milisecond barrier has been broken.

code here

All
  Part 1: OK (0.26s)
    958  μs ±  45 μs, 922 KB allocated, 8.2 KB copied,  41 MB peak memory
  Part 2: OK (0.23s)
    955  μs ±  51 μs, 4.8 MB allocated, 3.3 KB copied,  42 MB peak memory

4

u/prafster Dec 05 '25

[LANGUAGE: Python]

I wrote a neighbours function for a previous year. It's been repeatedly useful.

Although my solution is simple it took me ages to realise that dots need to be excluded. Doh!

def accessible(input):
    result = []
    for r in range(0, len(input)):
        for c in range(0, len(input[r])):
            if tile(input, (r, c)) == EMPTY:
                continue

            adj = neighbours((r, c), input, include_diagonal=True)

            if sum(tile(input, pos) == PAPER for pos in adj) < 4:
                result.append((r, c))

    return result


def part1(input):
    return len(accessible(input))


def part2(input):
    result = 0

    while True:
        removable = accessible(input)

        if len(removable) == 0:
            break

        result += len(removable)

        for pos in removable:
            input[pos[0]] = replace(input[pos[0]], pos[1], EMPTY)

    return result

Full solution

4

u/vitelaSensei Dec 05 '25 edited Dec 05 '25

[LANGUAGE: Haskell]
I thought this was the easiest day since day 1, there were virtually no edge cases

part2 grid = size grid - size (removeAll grid)
  where
  removeAll grid = let !next = filter (\pos -> getAdjacentRolls grid pos >= 4) grid
                    in if size next == size grid then grid else removeAll next
  getAdjacentRolls grid pos = length . filter (`member` grid) . map (pos +) 
                            $ [V2 a b | a <- [-1..1], b <- [-1..1], a /= 0 || b /=0 ]

Here's the full version, part 2 runs in 96ms, it ain't brilliant but I'm striving for idiomatic haskell rather than performance

4

u/gisikw Dec 05 '25

[LANGUAGE: vim]

Figured this was the right puzzle to try it on :) Minor printf preprocessing to support comments and \x escape codes, and part two takes an hour. But it's enjoyable to watch! We assume the input file as the initial buffer, with the part number prepended at the top :)

Parts 1 & 2

:s/2/@r/e\x0dV"gDG # @g is 1 for part 1, @r for part 2
ohk\x16\x162j2lyGpVjjgJ04l"zx0:s/[^@x]//ge\x16\x0d:s/.*/\\=(strlen(submatch(0))<4)?1:0\x16\x0d"zp:s/1@/1x/e\x16\x0d$"zxx\x16\x0f\x16\x0fjlx"zPl\x1b0v$"addd # @a will swap a @ for x, but only if you have <4 neighbors
ox@aj0l\x1b:s/x/\\=strlen(getline(2))\x1b0v$"bddd # @b will call @a for each char in the row
ox@b\x1b:s/x/\\=line('.')-1\x1b0v$"cddd # @c will call @b for each row in the grid
oGVggyGpVGgJ:s/[^@]//ge\x16\x0d:s/.*/\\=strlen(submatch(0))\x16\x0d0v$"fddd\x1b0v$"eddd # @e will count all @'s in the buffer and store it in @f
o:%s/x/./g\x16\x0dggjl\x16\x1b@c@r\x1b0v$"rddd # @r will replace x's with . or crash, then call @c, then recurse
yypVr.ggyyPVr.\x16GI.\x1bgg$\x16GA.\x1b # wrap the grid with empty spaces on all sides
Go\x1b@e # count the grid once
ggjl\x1b@c@g # traverse the grid, then maybe to part two
Go\x1b"fp@eGA-\x1b"fp:s/.*/\\=eval(submatch(0))\x0d # compute the difference between the new @ count
ZZ # bye

2

u/hugh_tc Dec 04 '25

[LANGUAGE: Python 3]

There is a clean-ish way to detect convergence in a Python: use itertools.accumulate to generate the (infinite) sequence x, f(x), f(f(x)), etc. and then use itertools.pairwise to find the point at which the sequence stabilises.

https://github.com/hughcoleman/advent-of-code/blob/main/2025/04.py

2

u/RojerGS Dec 04 '25

I LOVE itertools and I try to shove it wherever I can and not even I would do it that way 🤣

1

u/se06745 Dec 04 '25 edited Dec 04 '25

1

u/daggerdragon Dec 04 '25

Need one more little dot to turn that period into a colon in order to comply with AutoMod's request XD

→ More replies (1)

1

u/ponycelebration Dec 04 '25

[LANGUAGE: Elixir] day_4_paste

Rather than a map, parsed the input and only retained "@" coordinates in a (Map)set. `do_remove/2` is inefficient but got the job done.

1

u/daggerdragon Dec 04 '25

Psst: we can see your Markdown.

1

u/danvk Dec 04 '25 edited Dec 04 '25

[Language: Haskell]

Full solution: https://github.com/danvk/aoc2025/blob/main/y2025d04/Main.hs

I typically find it better to model AoC grid problems as maps from (x, y) -> char, rather than as nested arrays. The advantage is that it feels natural to look up (x, y) pairs, and you can expand the grid to negative indices or model it sparsely as needed. That wasn't at all needed today, but at least I have my grid code for future days now.

The only really interesting bit here is the iterateUntilStall function. Haskell people: feedback is very welcome!

-- Iterate until another function stops changing
iterateUntilStall :: (Eq b) => (a -> a) -> (a -> b) -> a -> (Int, b, a)
iterateUntilStall stepFn stallFn initState = (n, stallVal, state)
  where
    states = iterate stepFn initState
    tuples = zip (map stallFn states) $ zip [0 ..] states
    (stallVal, (n, state)) = firstStall tuples
    firstStall (hd@(s1, _) : tl@((s2, _) : _)) = if s1 == s2 then hd else firstStall tl
    firstStall _ = error "no stall"

2

u/daggerdragon Dec 04 '25 edited Dec 04 '25

Comment temporarily removed.

Your "interesting bit" code block is too long for the megathreads. Since you already have a link to your full code, please edit that oversized code block down to ~5 lines or just link directly to the appropriate line of code(s) in your full solution. When you do this, I will re-approve your comment. edit: 👍

→ More replies (1)

1

u/Fresh_Mission_9745 Dec 05 '25

1

u/daggerdragon Dec 05 '25

I've already informed you 2 days ago about including puzzle inputs in your repo. I still see full plaintext puzzle inputs in your repo. e.g.

https://github.com/IlanWanounou/AdventofCode-2025/blob/master/adventofcode/Day1/input.txt

Remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. Do not post again in /r/adventofcode until you have done so.

1

u/Own_Sail1927 Dec 05 '25 edited Dec 05 '25

[LANGUAGE: Python]

Commenting for ppl trying to understand solution

# --- Day 4: Lobby - Part 2 ---
file_path = '/content/drive/MyDrive/Colab Notebooks/AOC-2025-Inputs/Q4P1_Input.txt'

def determine_next_state(grid, row, col):
  """
  Checks the 8 neighbors of a cell.
  If an '@' cell has less than 4 '@' neighbors, it transforms to 'x'.
  If it has 4 or more '@' neighbors, it remains '@'.
  Returns: (new_character, has_changed_boolean)
  """
  neighbor_count = 0
  # Check 3x3 grid around the cell
  for i in range(row-1, row+2):
    for j in range(col-1, col+2):
      # Boundary checks and skip the cell itself
      if(i < 0 or j < 0 or i >= len(grid) or j >= len(grid[i]) or (i == row and j == col)):
        continue
      
      # Skip empty floor
      if(grid[i][j] == '.'):
        continue
      # Count occupied neighbors
      elif(grid[i][j] == '@'):
        neighbor_count += 1
        # Optimization: If we hit 4, we know the result immediately
        if neighbor_count >= 4:
          return '@', False # Stays as '@', no change
  
  # If loop finishes, neighbor_count is < 4. The cell changes.
  return 'x', True # Transforms to 'x', change occurred

current_grid=[]
# Read and parse file into a 2D list of characters
with open(file_path, 'r') as f:
  content = f.read().split()
  # Convert list of strings into list of lists (mutable grid)
  for item in content:
    current_grid.append(list(item))

# Create a deep copy for the next generation's state
next_grid = [inner_list[:] for inner_list in current_grid]
changes_in_round = -1
total_changes = 0

# Run simulation until the grid stabilizes (no changes in a round)
while changes_in_round != 0:
  changes_in_round = 0
  
  for r in range(len(current_grid)):
    for c in range(len(current_grid[r])):
      cell = current_grid[r][c]
      
      if(cell == '.'):
        continue
      elif(cell == '@'):
        # Calculate what happens to this cell based on current neighbors
        new_char, changed = determine_next_state(current_grid, r, c)
        next_grid[r][c] = new_char
        
        if(changed):
          changes_in_round += 1
          
  # Update the current grid to match the calculated next state
  current_grid = [inner_list[:] for inner_list in next_grid]
  total_changes += changes_in_round

print(total_changes)

1

u/daggerdragon Dec 06 '25

Comment temporarily removed.

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment.

1

u/vgamula Dec 05 '25

2

u/daggerdragon Dec 06 '25

I've already informed you before about including puzzle inputs in your repo. I still see full plaintext puzzle inputs in your repo. e.g.

https://github.com/vgamula/advent-of-code/blob/main/2021/22/input1.txt

Remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. Do not post again in /r/adventofcode until you have done so.

1

u/TheAgaveFairy Dec 07 '25

[Language: Mojo]

https://github.com/TheAgaveFairy/AoC25Mojo/blob/main/src/day04.mojo

GPU solution: full convolution where each GPU block gets a 16x16 tile (and 16 x 16 threads), pulled into local memory with a halo of 1.

Not the fastest solution but I wanted the challenge! I'm tempted to generate some bigger inputs and see how it scales. I'm also curious about implementing a simple im2col solution for fun :).

1

u/daggerdragon Dec 08 '25

I've already informed you before about including puzzle inputs in your repo. I still see full plaintext puzzle inputs in your repo. e.g.

https://github.com/TheAgaveFairy/AoC25Mojo/blob/main/inputs/full01.txt

Remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. Do not post again in /r/adventofcode until you have done so.

1

u/Prof_Farnsworth1729 Dec 10 '25

[Language: Javascript]

[Red(Dit) One]

I decided that the oldest technology that fits with me doing Javascript is Netscape, I managed to find a 3.0 version compatible with Windows 11. Had to sacrifice a lot of modern stuff including ability to have new lines in an input

Code

1

u/daggerdragon Dec 12 '25

Netscape

Good lord 😅 Who needs newlines on the Internet anyway >_>

3

u/Nathanfenner Dec 04 '25

[LANGUAGE: TypeScript]

Useful fact for TS/JS: a Set can be used as a queue, because it's totally legal to s.remove(x) an item while you're iterating over a for (const x of s) { ... }. You can even s.add(x) back in later - if already present, this has no effect, otherwise it's placed at the end of the queue.

→ More replies (1)

3

u/ricbit Dec 04 '25

[LANGUAGE: Python]

For part1, my lib did most of work, but I was happy to use the for-else construct:

def remove_layer(table):
  visited = []
  for j, i in table.iter_all():
    if table[j][i] != "@":
      continue
    count = 0
    for jj, ii in table.iter_neigh8(j, i):
      if table[jj][ii] == "@":
        count += 1
        if count >= 4:
          break
    else:
      visited.append((j, i))
  for j, i in visited:
    table[j][i] = "."
  return len(visited)

Part 2 was simple, just call part1 until no more changes happen.

def remove_all(table):
  ans = 0
  while (incr := remove_layer(table)) > 0:
    ans += incr
  return ans

3

u/abnew123 Dec 04 '25

[Language: Java]

Very straightforward, no real tricks in my solution. Just iterate through every roll and check all it's neighbors, then do it until nothing changes for part 2

Solution

Live Solve

3

u/YenyaKas Dec 04 '25

[LANGUAGE: Perl]

Standard 2D map walking, one needs to be careful to not overflow/underflow the map coordinates, and in part 2 to stash rolls to remove, and remove them only after the full map is searched.

Part 1, Part 2.

3

u/bilgincoskun Dec 04 '25

[LANGUAGE: Python]

The Code

Used numpy. Initially I copied the array in every iteration in the second part but ultimately It does not matter for the result so the grid can be modified in-place.

→ More replies (3)

3

u/EffectivePriority986 Dec 04 '25

[Language: perl]

I used my pre-built AoC Grid library. The downside is, I forgot how it worked and didn't document it well enough.

Main code

Grid library

3

u/Kehvarl Dec 04 '25

[LANGUAGE: Python3]

Part 1 gave me a problem because I kept trying to troubleshoot my approach instead of looking for a typo (and maybe it's time to learn to use complex numbers for coordinates?). Once I finally realized that X is not Y, I had a solution in short order.

Part 2 was a matter of making part 1 something I could call repeatedly, and then once again looking for a simple typo. In this case I wasn't properly removing the removed rolls. Fortunately once that was handled the approach went quickly enough that I didn't need to look for more clever solutions.

Part 2

3

u/TreeSquirrles Dec 04 '25

[Language: Python]

First time I actually solved it really quickly.

I think it took me less than 5 minutes from when I started to when I finished part 2.

https://github.com/TreeSquirrles/Advent-of-Code/blob/main/2025/4/1and2.py

Brute force solution, I don't know how it could be solved quickly. I got the answer anyway.

→ More replies (1)

3

u/ThreeHourRiverMan Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Golang]

paste

part1 536.292µs

part2 9.492375ms

3

u/bofstein Dec 04 '25

[LANGUAGE: Google Sheets]

This is just brute force. For Part 1 I made a grid and checked for the specified conditions in each cell, very simple. For Part 2 I considered if there was some clever way to figure out which rolls would never be removed and count the rest, but when I couldn't think of anything quickly, I just copied the grid a bunch of times until the sum of removed rolls was 0. It took much more iterations in the real input of course but still pretty easy and quick to do, especially as once I pasted a few grids I could copy those and paste a bunch at a time.

https://docs.google.com/spreadsheets/d/1HICW_BnUpI_z4QboU3vMjWa6mZD7EmfJ7vA18DBn-qI/edit?usp=sharing

3

u/Gryphon-63 Dec 04 '25 edited Dec 04 '25

[Language: Swift]

Solution

Spent several minutes trying to figure out what could possibly be wrong with what is a fairly simple Part 1 solution before realizing that I had forgotten to check whether the location I was counting the neighbors of had a roll of paper in it.

Wondered if I should try a more sophisticated algorithm for part 2, decided to try the obvious solution first & it turned out that ran fast enough.

3

u/an1gh7 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: numpy]

def load_data(filename):
    with open(filename, 'r') as f:
        for line in f:
            line = line.rstrip('\n')
            yield list(line)

import numpy as np
a = np.array(list(load_data('input.txt')))

# Part One

def accessible(a):
    padded = np.pad(a, 1, mode='constant', constant_values='.')
    k = np.array([[*'@@@'], [*'@.@'], [*'@@@']])
    swv = np.lib.stride_tricks.sliding_window_view(padded, k.shape)
    neighbours = np.sum(swv == k, axis=(2, 3))
    return np.logical_and(neighbours < 4, a == '@')

print(np.sum(accessible(a)))

# Part Two

total = 0

while True:
    to_remove = accessible(a)
    if np.sum(to_remove) == 0:
        break
    total += np.sum(to_remove)
    a[np.where(to_remove)] = '.'

print(total)

3

u/WillMatt Dec 04 '25

[Language: Go]

GitHub

3

u/naclmolecule Dec 04 '25

[LANGUAGE: Python]
Not gonna pass on an excuse to use convolutions:

DATA = np.array([[int(c == "@") for c in row] for row in RAW.splitlines()])
KERNEL = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])

def part_one():
    neighbors = convolve(DATA, KERNEL, mode="constant")
    return np.logical_and(DATA, neighbors < 4).sum()

def part_two():
    rolls = DATA.copy()

    while True:
        to_remove = np.logical_and(rolls, convolve(rolls, KERNEL, mode="constant") < 4)
        if not to_remove.any():
            return DATA.sum() - rolls.sum()
        rolls -= to_remove

3

u/_rabbitfarm_ Dec 04 '25

[Language: Perl]

My approach involved manipulating the puzzle grid, a nested array, something I find easiest to do in Perl!

Part 1: https://adamcrussell.livejournal.com/63445.html

Part 2: https://adamcrussell.livejournal.com/63621.html

3

u/KyxeMusic Dec 04 '25 edited Dec 04 '25

[Language: Go]

Not gonna lie I was expecting something brutal in part 2. My adjacent counting function is inefficient but I wanted to keep it readable

https://github.com/kikefdezl/advent-of-code/blob/main/2025/day_04/main.go

3

u/ktwrd Dec 04 '25 edited Dec 04 '25

[LANGUAGE: C#]

Back again with more low-memory C# code. Today's solution uses 330KiB of memory (for my puzzle input) and takes about 8ms to run (with AOT, ~21ms without)

adventofcode/2025/Day4.cs at main · ktwrd/adventofcode

→ More replies (1)

3

u/AlexTelon Dec 04 '25 edited Dec 04 '25

[Language: python]

python 19 lines of code

Storing the changes made in each iteration so the answer in the end is just:

print(len(changes[0]), sum(map(len,changes)))

I attempted to use the builtin ChainMap but it was too slow. It would have been a fun trick to make the solution even shorter.

I also used a hack to count what I call local instead of adjecent. Returning the original point itself avoids an extra check, that and using a defaultdict makes that function very simple.

Edit: Here is a cleaned up solution!

python 8 lines

My inner loop is just this now:

sizes = []
while rolls != (rolls := {r for r in rolls if local_rolls(*r) > 4}):
    sizes.append(len(rolls))

The use of the := allows me to update rolls and check if its different from the last iteration.

An alternative would be this python 8 lines:

sizes = []
while remove := {r for r in rolls if local_rolls(*r) < 5}:
    rolls = {r for r in rolls if r not in remove}
    sizes.append(len(remove))

Which is nicer overall but not as fun!

Edit2: Probably an even cleaner version python 8 lines

I forgot about - aka difference on sets so using that instead of the explicit set comprehension. But also using filter to remove another set comprehension and hide the "business logic" behind a nice can_take function.

Now the core loop is:

while remove := set(filter(can_take, rolls)):
    rolls -= remove
    sizes += [len(remove)]

3

u/light_ln2 Dec 04 '25

[Language: Python]

My 8-liner, grid is represented as set of points, to avoid boundary checks

grid = open('in04.txt').readlines()
grid = {(r,c) for r in range(len(grid)) for c in range(len(grid[0])) if grid[r][c] == '@'}
neigh = lambda r,c: sum((r+dr,c+dc) in grid for dr in [-1,0,1] for dc in [-1,0,1] if dr or dc)
avail = lambda: {(r,c) for r,c in grid if neigh(r,c) < 4}
print(len(avail()))
total = len(grid)
while len(avail()): grid -= avail()
print(total - len(grid))

3

u/CutOnBumInBandHere9 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Python]

Is it cheating to get a library to do most of the heavy lifting?

Part 1:

data = load(4, "chararray")
count = scipy.ndimage.convolve(1 * (data == "@"), np.ones((3, 3)), mode="constant")
p1 = ((data == "@") & (count < 5)).sum()

For part 2, I basically just ran the last two lines in a loop. Here's the full code along with any imports and a tiny bit of text explaining my thoughts

3

u/lost_in_a_forest Dec 04 '25 edited Dec 04 '25

[Language: Uiua]

Day 4 of learning uiua. Works by shifting the grid in eight different directions and adding the shifted grids. Wherever the sum is < 4 is accessible.

⊜(=@@) ⊸≠@\n &fras "../inputs/day04.in"

A ← ×<4 ⊸(/+≡⌟⬚0↻ ⊂C₂A₂)

∩(⧻⊚) ⊃A(- ⊸⍥(- ⊸A) ∞)
→ More replies (1)

3

u/trevdak2 Dec 04 '25 edited Dec 04 '25

[Language: Javascript]

Always golfing. Adjusted to be input-width agnostic, since the width appears to be variable.

Part 1, 133 bytes:

z=$('*').innerText
N=z.indexOf`\n`
s=a=>z.substring(a,a+3).split`@`.length;[...z].filter((v,i)=>v=='@'&s(i-N-2)+s(i-1)+s(i+N)<8).length

Part 2, 164 bytes:

z=$('*').innerText
k=N=z.indexOf`\n`
C=(c=(x=z)=>x.split`@`.length)()
s=a=>c(z.substring(a,a+3))
for(;k--;)z=[...z].map((v,i)=>s(i-N-2)+s(i-1)+s(i+N)>7?v:0).join``
C-c()
→ More replies (10)

3

u/im_sofi Dec 04 '25

[LANGUAGE: Python]

My fastest day yet! Grid problems go quick, especially those asking about neighbors, since these just scream as a convolution to a mask problem to me.

https://codeberg.org/soupglasses/advent-of-code/src/branch/main/2025/day_04.py

3

u/raevnos Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Common Lisp]

paste

3

u/pedantic_git Dec 04 '25

[LANGUAGE: Ruby]

Love it when I can solve a puzzle this far in within 45 minutes of my alarm going off in the morning. Thanks to my trusty `grid.rb` that has been growing over the years of solving AoC!

https://github.com/pedantic-git/aoc2025/blob/main/04/04.rb

https://github.com/pedantic-git/aoc2025/blob/main/utils/grid.rb

3

u/Zinkerino Dec 04 '25

[LANGUAGE: google sheets]

Paste input to B2 and delete all empty rows after the input

Fill C1 with

=map(B1:B,lambda(p,ArrayFormula(mid(p,sequence(1,len(p)),1))))

Fill A2 with

=MAP(B2:B,lambda(p,
  join("", map(sequence(1,len(p)),
    lambda(i,
      if( offset(B2,row(p)-2,i)="@",
        if( countif( offset(B1,row(p)-2,i-1,3,3), "@" )<=4,
          1, "@"
        ),
        offset(B2,row(p)-2,i)))
      )
    )
  )
)

Part 1 answer is

=len(regexreplace(join("",A2:A),"[^1]",""))

For part 2, enable iterative calculation (File -> Settings -> Calculation), may need to set higher limit (50 might not be enough)

Overwite C1 with

=map(A1:A,lambda(p,ArrayFormula(mid(p,sequence(1,len(p)),1))))

Change any cell to trigger recalculation, then wait until all calculation resolves, answer formula is same as part 1

3

u/ednl Dec 04 '25

[LANGUAGE: Python]

FORKLIFT IS (game of) LIFE!

Part 2 using convolve2d from scipy. Taking the complete sum repeatedly is wasteful, so it's not quick. https://github.com/ednl/adventofcode/blob/main/2025/04.py

import numpy as np
from scipy.signal import convolve2d

with open('input') as f:
    state = (np.array([list(i.strip()) for i in f]) == '@').astype(np.uint8)

# Leave centre bit on, so change neighbour check from <4 to <5
kernel = np.ones((3, 3), dtype=np.uint8)

prev = 0
init = count = np.sum(state)
while prev != count:
    prev = count
    nb = convolve2d(state, kernel, mode='same')
    state = state & (nb > 4)  # leave rolls that have more than 3 neighbours
    count = np.sum(state)
print(init - count)  # total removed
→ More replies (2)

3

u/ap29600 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: K]

I recycled the famous game of life one-liner by John Scholes

rot:{,/(x_y;x#0)@<0,x}

input:"@"=0:"4.txt"
2+//{x&5>2+//2(-1 0 1 rot/:\:)/x}input
2+//input>{x&4<2+//2(-1 0 1 rot/:\:)/x}/input

part 2 could be more efficient by only working on the cells that have had a neighbour die, but this is fast enough at 80ms.

A slightly golfed down variant:

conv:2+//2(-1 0 1{,/(x_y;0*x#y)@<0,x}/:\:)/
input:"@"=0:"4.txt"
2+//input&5>conv input
2+//input>{x&4<conv x}/input

Edit: I caved and did the faster version, this takes 3.5ms on the given input (excl. startup time and reading the file)

pad:{x:0,'x,'0;p,x,p:0*1#x}
input:pad"@"=0:"4.txt"
adj:((,/+/:\:/(1((#*input)*)\-1 0 1))^0)+\:

input:,/input
alive:input
count:+/input@adj@!#input
erode:{
  alive[x]:0
  count[i:,/adj x]-:1
  ?{(alive x)&4>count x}#i}

+/input&4>count
z:erode/&input&4>count
+/input>alive

3

u/bakibol Dec 04 '25

[LANGUAGE: Python]

Nothing fancy.

Topaz's paste

3

u/AtomPhys Dec 04 '25

[LANGUAGE: q]

Same basic method for both parts, work through indices, check surrounding positions. Part 2 just iterates the process until no more changes are found.

 / p1
q)sum {$["@"=t[y;x];4>sum (t[y-1;x-1];t[y-1;x+1];t[y+1;x-1];t[y+1;x+1];t[y;x-1];t[y;x+1];t[y-1;x];t[y+1;x])="@";0b]} .' (til count t) cross (til count t)
    
/ p2
q)c:()
q)f:{[m] c,::enlist {[x;y;m] $["@"=m[x;y];4>sum (m[x-1;y-1];m[x+1;y-1];m[x-1;y+1];m[x+1;y+1];m[x-1;y];m[x+1;y];m[x;y-1];m[x;y+1])="@";0b]}[;;m] .' (til count m) cross (til count m);(count m;count m)#?[last c;"x";raze m]}
q)(f\)t
q)sum sum each c

3

u/acquitelol Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Elle]

https://github.com/acquitelol/aoc2025/blob/mistress/solutions/04/solution.le

Took a little bit of effort to make this efficient, solves both parts in 12.7ms on my M1 Mac.

→ More replies (2)

3

u/Cute-Document3286 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Zig 0.15]
Part 1: 33 μs
Part 2: 232 μs

Optimized using SIMD scanning, unrolled neighbors checks and static buffers
(Apple M4, mac book pro. `ReleaseFast` compilation preset)

https://github.com/gabrielmougard/AoC-2025/blob/main/04-printing-department/main.zig

→ More replies (5)

3

u/Ok-Bus4754 Dec 04 '25

[Language: Python+rust]

port of my earlier python solution, using sets to store the grid

https://github.com/Fadi88/AoC/tree/master/2025/days/day04

runtimes on Windows using an i9 13th gen
Interesting how rust debug is slower than python

ImplementationPart 1 TimePart 2 Time
Python9.04 ms246.65 ms
Rust (Debug)12.95 ms287.02 ms
Rust (Release)1.33 ms28.84 ms

3

u/erikade Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Go]

Today was a classic AoC cellular automaton challenge that felt perfect for some old-school optimization techniques.

Performance: 6.5ms end-to-end (M1 MacBook Air)

Link to GitHub

The accompanying write-up is here

Comments welcome on r/adventofcode

Happy Coding!

3

u/anaseto Dec 04 '25

[LANGUAGE: Goal]

Nice array-friendly one today.

i:".@"?""\=-read"i/04"      / parsed input (1 for rolls)
nbsum:2{+(x+0,-1_x)+1_x,0}/ / neighbor roll count in 8 dirs
f:{x&5>nbsum x}             / forklift-accessible rolls
say+//f i / part1
say+//f'{x&~f x}\i / part2

Just fits in a single line and 80 chars if golfed without comments and spacing:

f:{x&5>2{+(x+0,-1_x)+1_x,0}/x};say'(+//f i:".@"?""\=-read"i/04";+//f'{x&~f x}\i)

3

u/jinschoi Dec 04 '25

[Language: Rust]

Over the years, I've implemented some utilities for AoC-like puzzles which made this one pretty quick. Part 2:

use aoc_utils::grid::*;

fn accessible(g: &Grid<char>) -> impl Iterator<Item = Pos> {
    g.all_positions(|&c| c == '@')
        .filter(|&pos| g.all_neighbors(pos).filter(|&npos| g[npos] == '@').count() < 4)
}

fn remove_accessible(g: &mut Grid<char>) -> usize {
    let rolls = accessible(g).collect::<Vec<_>>();
    for &pos in &rolls {
        g[pos] = '.';
    }
    rolls.len()
}

fn main() {
    let mut g = Grid::<char>::read_from_file("input.txt").unwrap();
    let mut res = 0;
    loop {
        let removed = remove_accessible(&mut g);
        res += removed;
        if removed == 0 {
            break;
        }
    }
    println!("{res}");
}

3

u/IvanR3D Dec 04 '25

[LANGUAGE: javascript]

My solutions: https://ivanr3d.com/blog/adventofcode2025.html#4

My solution (to run directly in the input page using the DevTools console).

3

u/sondr3_ Dec 04 '25

[LANGUAGE: Haskell]

Pretty happy with mine today, I remembered fuzzing with similar tasks last year but saw people use iterate to handle the recursion and wanted to do that today as well. I did not think about what part 2 should be so had to refactor a bit, but with my coordinate and grid helpers it was pretty easy today.

data Cell = Paper | Empty
  deriving stock (Show, Eq, Ord, Generic)
  deriving anyclass (NFData)

type Input = Grid Cell

partA :: Input -> Answer
partA xs = IntAnswer . length . fst $ clear xs

partB :: Input -> Answer
partB xs = IntAnswer . length $ concatMap fst $ takeWhile (\(c, _) -> not . null $ c) $ iterate (\(_, acc) -> clear acc) (clear xs)

clear :: Input -> ([Cell], Input)
clear xs = Map.mapAccumWithKey go [] xs
  where
    go acc pos Paper = if numPapers pos xs < 4 then (Paper : acc, Empty) else (acc, Paper)
    go acc _ Empty = (acc, Empty)

numPapers :: Position -> Input -> Int
numPapers pos g = length $ filter (== Just Paper) $ filter isJust $ papers g pos

papers :: Input -> Position -> [Maybe Cell]
papers g pos = map (`find` g) (neighbours pos allDirs)

parser :: Parser Input
parser = gridify <$> (some (Paper <$ symbol "@" <|> Empty <$ symbol ".") `sepBy` eol) <* eof

Decently quick, but since I simulate the whole grid every removal it's not the fastest.

All
  AoC Y25, day 04 parser: OK
    6.19 ms ± 429 μs
  AoC Y25, day 04 part 1: OK
    6.95 ms ± 483 μs
  AoC Y25, day 04 part 2: OK
    186  ms ±  16 ms

3

u/___ciaran Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Go]

https://codeberg.org/cdd/aoc/src/branch/main/2025/go/4.go

I had fun with this problem. At first I used a naive iterative approach. I was able to optimise part 2 a bit by switching to a recursive search.

Part 1 runs in ~280µs and part 2 runs in ~680µs on my laptop, which I'm pretty happy with.

3

u/jad2192 Dec 04 '25

[LANGUAGE: Python]

A "classy" solution using python's built in complex number type to represent 2d coordinates, was able to use part 1 for part 2 by adding a store of the accessible roll coordinates.

Github Link

3

u/evans88 Dec 04 '25

[LANGUAGE: Python]

Very happy with my solution, I think it's very readable. I also used some of my previous year's code to handle directions checks as sum of tuples

paste

3

u/Klutzy_Bear_579 Dec 04 '25

[LANGUAGE: Kotlin]

Here is my solution for Day 4.

3

u/TheZigerionScammer Dec 04 '25

[Language: Python]

Fairly easy one today, the theme was sets. For the first part I converted the input into a set with the coordinates of all the rolls in it. Then I iterated over that set, set it up so it would generate a check set of all 8 neighbors of each roll, then intersect that set with the original rolls set and count it if the resulting set had less than 4 members. When I saw Part 2 I moved that code into a while loop, and also added every removable roll into the remove set and subratcted it from the main roll set at the end of each loop. Once it went through and couldn't find any more rolls to removed it's done. Got both stars quickly and no wrong answers, thank goodness.

Paste

3

u/Teem0WFT Dec 04 '25

[LANGUAGE: Julia]

AoC_2025_Julia_day4

My worst code so far. I'm not familiar with bitarrays, and I pretty much bruteforced the thing without consideration for memory allocations.

AMD Ryzen 5 CPU laptop :

part 1 : 0.024561 seconds (100.33 k allocations: 14.375 MiB, 28.72% compilation time)

part2 : 5.226422 seconds (172.55 M allocations: 5.153 GiB, 11.17% gc time)

Set operations on binary tuples would have been much much more efficient, now that I see other people's solutions.

3

u/SurplusSix Dec 04 '25

[LANGUAGE: gleam] https://github.com/alsm/aoc2025/blob/master/src/day04.gleam Realized I didn't need to store anything for empty spaces. With part2 there is a recursive fn that finds the accessible rolls, removes them from the dict and calls itself with the new map and how many were removed, once we can't remove any more than returns the total number of rolls removed.

3

u/ednl Dec 04 '25 edited Dec 04 '25

[LANGUAGE: C]

https://github.com/ednl/adventofcode/blob/main/2025/04alt.c

I made a new version with BFS instead of repeated loops over the whole grid. That was a bit more work while looking for my old queueing functions, but 3 to 5 times faster! Best times are now: 244 µs on an Apple M4, 601 µs on a Raspberry Pi 5 (internal timer, not including reading from disk).

// Part 1: fill the queue with all paper rolls to be removed
for (int i = 1; i < N + 1; ++i)
    for (int j = 1; j < N + 1; ++j)
        // Is it a paper roll, and not in the queue yet, and doesn't have too many neighbours?
        // Then put it in the queue, and mark it as queued.
        check(i, j);
printf("%d\n", qlen);  // example: 13
// Part 2: remove, check neighbours, repeat until no more paper rolls removable
int removed = 0;
for (Vec pos; dequeue(&pos); ++removed) {
    grid[pos.row][pos.col] = SPACE;  // forklift action
    for (int i = -1; i < 2; ++i)
        for (int j = -1; j < 2; ++j)
            check(pos.row + i, pos.col + j);  // neighbours might be reachable now
}
printf("%d\n", removed); // example: 43

3

u/WolfRushHour Dec 04 '25

[LANGUAGE: Julia]

Matrix approach with CartesianIndices and OffsetArrays for the convolution.

# AoC 2025 Day 4

# imports
using OffsetArrays

# helpers
function convolve(M::Matrix, K::Matrix)
    K = OffsetArray(K, -2,-2)
    N = zeros(Int, size(M))
    for i=CartesianIndices(M)
        for j=CartesianIndices(K)
            if in(i+j, CartesianIndices(M))
                N[i] += M[i+j]*K[j]
            end
        end
    end
    return N
end

function remove_rolls(M::Matrix, K::Matrix, l::Int)
    N = copy(M)
    A = convolve(N, K).*N # adjacents to rolls
    for i=eachindex(N)
        if A[i]<=l
            N[i] = 0
        end
    end
    return N
end   

# main
function main()
    # parse input file
    input = "in_2025-12-04.txt"
    rollplan = parse.(Int, stack(replace.(readlines(input), "."=>"0", "@"=>"1")))

    # part 1
    limit = 3 # roll limit
    frame = [1 1 1;1 0 1;1 1 1] # kernel
    rollplan_new = remove_rolls(rollplan, frame, limit)
    rolls_rem = sum(rollplan - rollplan_new)

    # part 2
    totals_rem = rolls_rem
    while sum(rollplan - rollplan_new) != 0
        rollplan = rollplan_new
        rollplan_new = remove_rolls(rollplan, frame, limit)
        totals_rem += sum(rollplan - rollplan_new)
    end
        
    # output
    output = (rolls_rem, totals_rem)
    output |> println
end

main()

3

u/woond3r Dec 04 '25

[Language: OCaml]

https://github.com/KacperKopiec/advent-of-code/blob/main/2025/day4/part2.ml

Easy BFS solution, deffinitly not the faster and lost some functional magic here, this is my 4th day of writing in OCaml.

3

u/quetzelcoatlus1 Dec 04 '25

[Language: C]

Part 2: Cool thing is that we don't have to remove rolls only after calculating how many can we remove (like shown visually on task) - we can just remove them as we go once found removable one

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 256
char tab[MAX_SIZE][MAX_SIZE];
int w,h=1;

int remove_rolls(){
    int rolls=0;
    for(int i=1; i<h; i++){
        for(int j=1; j<=w; j++){
            if(tab[i][j]){
                int count=0;
                for(int a=-1; a<=1; a++)
                    for(int b=-1; b<=1; b++)
                        if(a || b)
                            count += tab[i+a][j+b];

                if(count < 4){
                    rolls++;
                    tab[i][j]=0;
                }
            }
        }
    }

    return rolls;
}

int main(int argc, char* argv[]){
     FILE* f = fopen(argc > 1 ? argv[1] : "input", "r");
     int sum=0, rolls;

     while(fscanf(f,"%s\n", &tab[h][1]) == 1){
         w=strlen(&tab[h][1]);
         for(int i=1; i<=w; i++)
            tab[h][i] = tab[h][i] == '@' ? 1 : 0;

         h++;
     }

     while((rolls = remove_rolls()) > 0) sum+=rolls;

     printf("%d\n",sum);

     return 0;
}

3

u/greycat70 Dec 04 '25

[LANGUAGE: Tcl]

Part 1, part 2.

Not sure if there's a clever way to do this. I just brute forced it. Part 2 ran in 0.555 seconds on my computer.

3

u/Dense-Virus-1692 Dec 04 '25

[LANGUAGE: Gleam]

I was kinda dreading these problems because my grid library wasn't up to snuff yet...

paste

3

u/SymmetryManagement Dec 04 '25

[Language: Java]

https://github.com/linl33/adventofcode/blob/year2025/year2025/src/main/java/dev/linl33/adventofcode/year2025/Day4.java

I parsed the input into an array of paper roll coordinates and a grid where each element stores the number of neighbors it has. The array of paper rolls are repeatedly iterated to find the ones with 4 or fewer neighbors.

Part 1 147us
Part 2 497us

I wanted to have a single method that can solve each part independently, so the extra bookkeeping part 2 requires slowed down part 1 quite a bit. There's probably still plenty of room for improvement, I tried to parse with simd but it ended up being slower.

3

u/JV_Fox Dec 04 '25

[LANGUAGE: C]

Had a bug in printing out the map to debug if the input function read it correctly which took me a while to solve. Since part 2 was just part 1 on repeat with deletion it was not much work to add the deletion and iterate over the function. Ensuring that all my functions perform one task had made the work for part 2 after part 1 a lot faster and cleaner.

Solution: Puzzle 4
Project: Embedded AoC

3

u/tcbrindle Dec 04 '25

[Language: C++23]

Our first 2-d grid puzzle of the year was, fortunately, pretty straightforward. Initially I modified a second copy of the grid for each iteration in part 2, but it turns out that you can get away with just modifying the original in-place to save a little run time.

Github: https://github.com/tcbrindle/advent_of_code_2025/blob/main/dec04/main.cpp

3

u/siddfinch Dec 04 '25

[LANGUAGE: Free Pascal]

Part 1 and Part 2

Another day, another "perfectly straightforward" solution documented to within an inch of its life. At this point, I'm one commit away from accidentally writing a small "Learning Free Pascal" book. The sort of book that appears mysteriously on library shelves, authored by no one, and printed in a typeface that looks suspiciously like an insurance actuary designed it.

I didn't do anything special. I merely conceived a design that, against all observable laws of software engineering, actually worked despite the complete lack of caffeine. After years away from Pascal, I've discovered something unnervingly comforting: the BEGINs, ENDs, and semicolons form just enough syntactic clutter for my brain to see what on earth it's doing finally. It's like the language gently marks all the edges for me. Odd. Also slightly condescending. But helpful.

I am getting way too poetic about a 55-year-old language.

3

u/CCC_037 Dec 04 '25

[Language: Rockstar]

Featuring Clint Eastwood taking the good, the bad, and the ugly!

part 1

4

u/CCC_037 Dec 04 '25

Very naive algorithm, just remove the rolls and iterate. Takes a few minutes to run.

part 2

3

u/icub3d Dec 04 '25

[LANGUAGE: Rust]

The day would definitely fit in the "easy" category for programming puzzles. My initial solution was probably similar to most where I just use the grid and check neighbors. I thought it might be fun to provide an alternative solution that is more set/graph based. I just track the rolls as points in a set and can then check neighbors. We can optimize p2 a bit by recognizing that the only candidates for removal are neighbors of removed nodes.

Solution: https://gist.github.com/icub3d/9daf52acd871eedec656e5a44ac61dd8

Video: https://youtu.be/f1Ng6hNjo5A

→ More replies (1)

3

u/aurele Dec 04 '25

[LANGUAGE: Uiua]

&fras"day4.txt"
⊜(=@@)⊸≠@\n
⊃⊢/+⍥(⟜(⧻⊚>)⊸(×>4⊸⬚0⧈(/+♭)≡˙⊟3_1_1))∞

You can try it in the Uiua pad by uploading your input file.

→ More replies (1)

3

u/CrAzYmEtAlHeAd1 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: Python]

Always remember to use sets instead of lists if you can create unique values 👍 Just found the locations of each roll and then used that to compare to the surrounding spaces. Maybe could speed it up a bit if I just did the check live instead of creating a set surrounding rolls and looping, but hey this one works albeit a little slow.

My solution on GitHub

3

u/dhawos Dec 04 '25

[LANGUAGE: Rust]

Managed to get it working pretty smoothly just counting neighbors for every tile in the grid.
I did not realize that for part 2 you can actually edit the grid while you're counting and it does not change the result so I could still optimize that.

At first I used a Grid implementation based on HashMaps but it was pretty slow, so I tried using a simple 1D Vector and it was much faster.

Final times :

Part 1 Answer: x
Time taken: 509.39µs
Part 2 Answer: x
Time taken: 3.21ms

My final code : https://gitlab.com/Dhawos/advent-of-rust/-/blob/d1190a3b61d34be14347fad8c969c3a1d5ee99ab/src/bin/2025-04.rs

A quick write-up I made : https://gitlab.com/Dhawos/advent-of-rust#2025-day-4

3

u/Daniikk1012 Dec 04 '25

[LANGUAGE: BQN]

Had to work around ⌽ wrapping around by surrounding the input with zeros during parsing. I like BQN, but man, this would've been so much easier in J, as it has !.0 to use fill elements instead of wrapping around, and ^:_ for convergence, instead of having to manually implement it using recursion. If I would add something to BQN, while I don't know what to do about !.0, it would be nice for ⍟∞ to work

Parse ← {𝕩⌾((1+↕≢𝕩)⊸⊑)0⥊˜2+≢𝕩}'@'=·>•FLines
Out   ← •Out"  "∾∾⟜": "⊸∾⟜•Fmt

Accessible ← (5>·+´(⥊1-↕3‿3)⌽¨<)⊸∧

•Out"Part 1:"
Out⟜(+´·⥊·Accessible Parse)¨"sample"‿"input"

•Out"Part 2:"
Out⟜(+´·⥊·-⟜{𝕊⍟(𝕩⊸≢)-⟜Accessible 𝕩}Parse)¨"sample"‿"input"

3

u/bmatcuk Dec 04 '25

[LANGUAGE: gleam]

https://github.com/bmatcuk/adventofcode2025/blob/main/day04/src/day04.gleam

I started part 1 using iv to build a 2D array, and considered continuing down that path for part 2. Ultimately, I decided to switch gears, removing iv completely:

I parse the input into a list of coordinates (#(x,y) tuples) for each paper roll. I then use that list as keys into a dictionary, where values are counts of neighboring paper rolls. The dictionary makes for slightly more efficient lookup of rolls by x,y coordinates, but may not have been strictly necessary - could probably have just used the list.key_* functions.

Anyway, the main recursive loop is: dictionary to kv list; partition list into rolls to remove (rolls with <4 neighbors) and remaining rolls; convert remaining rolls back to a dictionary; loop through the "rolls to remove" and update neighboring counts in the remaining rolls dictionary; recurse until no more rolls to remove, returning a count of the rolls removed.

Not super efficient, but, compilation plus part 1 + 2 combined runs in about a quarter of a second =)

3

u/SunMatrix64 Dec 04 '25 edited Dec 04 '25

[LANGUAGE: C++]

GitHub

I initially solved it using a simple scan of the grid, counting adjacent rolls and removing as I go.

I then tried creating a map<pair<>, set<pair<>>> graph to make part2 faster. Doing it this way allowed me to just check existing rolls on each pass instead of all the empty space, as well as just asking the roll how many adjacent rolls were in the set.

While this did make part 2 faster, the act of creating the graph by itself was significantly slower than the simple v1 solution, making the overall time ~10x slower. ~2-3ms -> 20ms+

3

u/TotalPerspective Dec 04 '25

[Language: Mojo]

https://github.com/sstadick/aoc-2025/blob/main/day04/main.mojo

Fully GPU solution. Used a very simply convolution kernel to do the counting. I thought I could be clever and put the whole input in global constant mem, which of course blew up in my face on part 2. I did the simple thing to just get part 2 done, but it is not optimal. I should have just gone with a normal tile-based setup from the start!

3

u/jaccomoc Dec 04 '25

[LANGUAGE: Jactl]

So much fun solving these problems in my own Jactl language.

Part 1:

Used a map for the grid to since map lookups return null for non-existent keys which makes the boundary searching easy. Stored true for squares where there is a roll of paper and false otherwise to make the checking for a roll of paper trivial. With a simple function to count the number of rolls of paper in neighbouring squares I just had to count how many entries in the grid had a roll of paper with less than four neighbours:

def grid = stream(nextLine).mapi{ line,y -> line.mapi{ ch,x -> [[x,y],ch == '@'] } }.flatMap() as Map
def adjCount(x,y) { [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]].filter{ dx,dy -> grid[[x+dx,y+dy]] }.size() }
grid.filter{ it[1] && adjCount(it[0]) < 4 }
    .size()

Part 2:

Once again a lazy brute-force solution was the easiest and simplest. Just iterate, removing rolls of paper with fewer than 4 neighbours, until the count stops changing. The only ugliness is using map() purely for a side-effect of mutating the grid when removing a roll of paper:

def grid = stream(nextLine).mapi{ line,y -> line.mapi{ ch,x -> [[x,y],ch == '@'] } }.flatMap() as Map
def adjCnt(x,y) { [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]].filter{ dx,dy -> grid[[x+dx,y+dy]] }.size() }
for (cnt = 0, pc = -1;
     cnt != pc;
     pc = cnt, cnt += grid.filter{ it[1] && adjCnt(it[0]) < 4 }
                          .map{ grid[it[0]] = false }
                          .size()) {
}
println cnt

Jactl on github

3

u/jpjacobs_ Dec 04 '25 edited Dec 04 '25

[Language: J] Solved in 3 ways (each less than a punchcard, I promise), fastest one (17ms for part 2 on my phone):

sh  =: >,{;~0 _1 1 [. par =: '.@'i.];._2 NB. shifts & parse
nb  =: (,#)@(i. (}.sh) +"1/~])@($#:I.@,) NB. neighbours
sol =: {{(, -&(+/) (](]-]*.4>+/"1@:{)^:m(<:>i.)@#)@nb)@par y}}
'`p1 p2'=: 1 sol`(_ sol)

Trick is finding the neighbours, by finding coordinates of rolls, looking up their coordinates in shifted variants. Then add a line for non-neighbours, where i. returned the length of the array. The solution then keeps the neighbours fixed and used for indexing in a boolean array indicating whether a roll is (still) there. Seems to be the fastest solution and what I've been using a lot for last year's AoC.

For fun, I also tried padding and convolving at each step using subarrays (13x slower at 241ms than above, but uses 3x less memory)

par =: '.@'i.];._2 [. pad =: 0&([,[,~[,.~,.) NB. Parse & Pad
accessible =: (1 1,:3) (4&{ *. 5>+/)@,;._3 ] NB. 3x3 windows, step 1
sol =: {{(+/@,@:- (- [: accessible@pad ])^:m)@par y}}
'`p1 p2'=:1 sol`(_ sol)

, and shifting around the array to create a 3D array, and work on that

sh  =: >,{;~0 _1 1 [. par =: '.@'i.];._2 NB. shifts & parse
sol =: {{(+/@,@:- (- [:({.*.4>+/@:}.) sh |.!.0])^:m)@:par y}}
'`p1 p2'=:1 sol`(_ sol)

At 25ms Still 40% slower than the top solution, likely because the array is shifted at every step.

→ More replies (1)

3

u/make_no_my_eye Dec 05 '25 edited Dec 05 '25

[LANGUAGE: Rust]

Feeling pretty good about this solution. Used a HashSet for quick lookups to see if a Point exists. Also gives me access to .contains() and .remove() which was nice.

I've been trying to be as functional as possible. Open to suggestions as always.

cargo run --release time:

- Part 1: Time: 1.941742ms

- Part 2: Time: 31.070285ms

(topaz paste)

3

u/Isti115 Dec 05 '25

[LANGUAGE: OCaml]

Here's my not at all optimal, but still good enough 50 line solution.

→ More replies (2)

3

u/AdamKlB Dec 05 '25 edited Dec 06 '25

[LANGUAGE: Rust]

https://github.com/NoSpawnn/advent_of_code_2025/blob/main/src/04/main.rs

Not sure how I feel about this one, initial solution took around 2 seconds to run in total but rewrote everything to use a 1D array (which I factored out/generalised so that'll be useful in future!) which took it down to around 3ms total

→ More replies (3)

3

u/vanZuider Dec 05 '25 edited Dec 05 '25

[LANGUAGE: Haskell]

paste

For part 1 I could put the accumulation array to good use; for part 2 the array-based solution gave the correct answer but was very slow. Using a map improved performance a bit.

EDIT:

part2 :: [String] -> Int
part2 ls = (removeRolls neighs)
    where h = length ls
        w = length $ head ls
        rolls = map fst $ filter (\(i,e) -> e=='@') $ zip [(y,x) | y <- [1..h], x <- [1..w]] (concat ls)
        neighs = merge preserveMissing dropMissing (zipWithMatched (_ _ a  -> a)) (Map.fromList $ zip rolls (repeat 0)) (Map.fromListWith (+) $ zip offsets (repeat 1))
        offsets = concatMap offset rolls

offset :: (Int,Int) -> [(Int,Int)]
offset i = map (tupadd i) $ filter (/=(0,0)) [(y,x) | y <- [-1..1], x <- [-1..1]]
    where tupadd (a,b) (c,d) = (a+c,b+d)

removeRolls :: Map.Map (Int, Int) Int -> Int
removeRolls a =
--    trace (show (length a) ++ " " ++ show (length removals))
    (if null removals then 0 else length removals + removeRolls a')
    where (removals, remains) = Map.partition (< 4) a
        a' = merge preserveMissing dropMissing (zipWithMatched (_ a b  -> a-b)) remains (Map.fromListWith (+) $ zip (concatMap offset $ Map.keys removals) (repeat 1))

I somehow managed to use the merge function despite still not fully understanding what an Applicative Functor is, and improved runtime to ca 0.33s.

3

u/onrustigescheikundig Dec 05 '25 edited Dec 05 '25

[LANGUAGE: Scheme (Chez)]

gitlab (~3 ms for Part 1, ~20 ms for Part 2)

I found grid and point libraries lying around from one of my previous years and tweaked them to work with my collections interface in my stdlib. The grid is backed by a persistent vec and is index by pairs of fixnums. The point library has functionality for 4- and 8-directional neighbors of a coordinate.

Part 1 just searches the grid for the number of @ tiles with fewer than 4 @ neighbors.

Part 2 turns the input grid into a map from coordinates to # of neighbors of each tile, with #f representing tiles without paper rolls. Then, it essentially does a BFS by finding all removable paper rolls (the "frontier"), removing them from the map, and decreasing the number of neighbors for each neighbor of each tile in the frontier. This process is repeated until no tiles are left in the frontier, and the resulting set of points remaining is returned.

I'll note that, for each step in the BFS, the entire grid is scanned to determine the frontier, and in that regard, it's not very algorithmically efficient. Nevertheless, it is faster than using the hash map structure that I wrote.

3

u/phord Dec 05 '25

[LANGUAGE: Python]

20-ish legible lines of code.

import sys
input = sys.stdin.read()
grid = {(i,j) for i,row in enumerate(input.split('\n')) 
            for j,col in enumerate(row) if col == '@'}

def neighbors(cell):
    x,y = cell
    return {(x+i, y+j) for i in range(-1,2) 
                    for j in range(-1,2) if i != 0 or j != 0}

def do_remove():
    global grid
    while True:
        cells = {c for c in grid if len(neighbors(c) & grid) < 4}
        if cells:
            grid -= cells
            yield cells
        else:
            break

print ("Part2: ", sum(len(cells) for cells in do_remove()))

3

u/nicuveo Dec 05 '25

[LANGUAGE: Haskell]

Having an advent of code library that includes 2D grid functions makes days like these very easy, so i haven't bothered actually making it efficient. ^^"

Full file on GitHub.

part1 :: Input -> Int
part1 g = countTrue do
  p <- allPoints g
  guard $ g ! p
  let n = countTrue $ gridEightNeighbours g p
  pure $ n < 4

3

u/JazzJassJazzman Dec 05 '25

[Language: Python 3]

Part 1

Part 2

My solutions aren't as clean as some I've seen, but they get the job done. I've been down this road before with Advent of Code, so I created an exception called NegativeIndex to avoid issues with how Python handles negative indices and avoid overcounting. That and the try-except block kept me from having any issues with indices. Other than that, it's pretty straightforward: find the @'s and check each neighbor. If it fits the condition, add 1 to the counter.

Part 2 was a good opportunity for me to practice recursion, especially while having to keep count of a variable while the recursion is happening. The code is nearly the same, but I created a function that takes the grid and a counter as arguments. Furthermore, the grid is updated to actually remove accessible rolls. Each round, the number of accessible rolls is set to 0. If the number is still 0 after checking the grid, the function returns the counter. If not, the function is called again with the updated grid.

3

u/ploki122 Dec 05 '25

[Language: T-SQL]

Github repo

Really slow (runs for ~90s on my setup), but it's probably a lot better than when I first tried to do a recursive query, instead of looping.

3

u/Meamoria Dec 05 '25

[Language: Kenpali]

Using AoC to test out my experimental minimalistic language.

Part 1

Part 2

For part 2, I realized that it doesn't matter exactly which order the rolls are removed in, as long as each removal is valid when we make it. So I didn't bother with keeping the iterations separate, like in the example; we just go through from top to bottom, removing rolls and updating as we go. Probably not the most efficient way to do it, but I'm happy if my solution doesn't hit the Kenpali website's two-minute timeout.

3

u/AlternativePeace1121 Dec 05 '25

[LANGUAGE: Java]

Part 1: Simple grid check O(m*n)

Part 2: Iterate over grid until number of rolls picked in iteration becomes 0 O(m * n * iteration)

Topaz link

2

u/charr3 Dec 04 '25

[LANGUAGE: Python]

A simple simulation problem. The main trick is to create dx/dy arrays so you can do a loop over the neighbors:

code

→ More replies (1)

2

u/psm_pm Dec 04 '25

[LANGUAGE: TypeScript]
Pretty easy day, p2 is a simple copy/paste of p1 with a few modifications. My neighbors method seems to be the same as u/charr3's. My fastest finish this year, following a dismal 30minute p1&p2 yesterday
https://pastebin.com/tiP8Lmr4

2

u/davidsharick Dec 04 '25

[LANGUAGE: Python]

Code

Pretty simple part 1 with a get_neighbors function in my library, part 2 was just sticking it in a while loop and modifying the grid.

2

u/alexbaguette1 Dec 04 '25

[LANGUAGE: Python]

Solution

2

u/Aspen138 Dec 04 '25

[LANGUAGE: Wolfram Mathematica]

I'd like to use ListConvolve to solve the problem.

paste

2

u/Ok-Bus4754 Dec 04 '25 edited Dec 04 '25

[Language: Python]

another easy day,
p1 explore around each roll and count, +1 for each that have less than 4
p2 same concept, expect those are removed in a while true loop till nothing can be removed

and of course used a set to store the data, make the code faster + no need to check for boundaries

p1 : 10 ms, p2 : 300 ms

https://github.com/Fadi88/AoC/blob/master/2025/days/day04/solution.py

will port to rust later and post the benchmarks of python vs rust debug vs rust release

2

u/cay_horstmann Dec 04 '25

[Language: Java] https://github.com/cayhorstmann/adventofcode2025/blob/main/Day4.java Took only a few minutes thanks to my CharGrid class from last year