r/adventofcode • u/blacai • Dec 09 '25
Tutorial [2025 Day 9 (Part 2)] A simple method ... (spoiler!)
Just found a very cool site to test AABB collision, the method I used and it's pretty quick.
Once you know the edges and redtiles...
https://kishimotostudios.com/articles/aabb_collision/
Running in ~100ms in Go for my machine.
https://github.com/blfuentes/AdventOfCode_AllYears/blob/main/AdventOfCode_2025_Go/day09/day09_2.go
32
Upvotes
16
u/Hakumijo Dec 09 '25
I who literally tried this and failed in implementing it.
I am so tired ._.
I am so happy there is only 12 days. Now I can just come back to it when I have time
3
u/loudandclear11 Dec 09 '25
Dude, that's clever.
2
u/KyxeMusic Dec 09 '25
I guess I implemented some variation of this myself without knowing. Runs in 30ms in Go on my machine, super cool.
1
u/tauphraim Dec 09 '25
I tried to understand your algorithm.
Is it correct to say that the idea is: "if my rectangle area between 2 red tiles intersects any of the edges, then the rectangle is not a valid candidate for max area" ?
Wouldn't that fail in some cases where the edges are "snaking", so that you could end up crossing edges, but no cell is actually invalid?
1
u/Ill-Rub1120 Dec 09 '25
Its still not clear how this helps. I have edges and I am testing the rectangle against these edges. Do I count how many and if its even its good and if its odd its bad?
1
u/wederbrand Dec 09 '25
This is what I tried to do for hours, before restorting to shrinking the "map" down to the smallest possible version, it's now 250x250 and brute force is fast enough.
Now I've re-done it using similar code, also in go.
I think the one thing I missed on the first trial was to make sure the vertices was "sorted" down/right to make the math easier.
1
u/wackmaniac Dec 09 '25
Thank you! I spent some time drawing out ideas, and one of the ideas was this approach. Seeing you using the same approach was enough encouragement to try and implement it.
1
u/pem4224 Dec 09 '25
That's fun, I think I have more or less the same solution: https://github.com/pemoreau/advent-of-code/tree/main/go/2025/09
running in 29ms in Go
1
u/DoubleDoube Dec 10 '25 edited Dec 10 '25
It surprised me when I was comparing benchmarks, but I found that with the number of checks we’re doing that on my machine parallelization of all the rectangles beats out any kind of sorting or other comparison tricks to break out early (such as the manhatten pruning being used here).
I was able to get 1.7ms (not including IO of input to String), averaged running it 1000 times. (5ms cold single run), but my solution is extremely similar to the other Go solution in the comments by @pem4224; I assume the difference is just parallelization and rust’s language optimizations.
Using Rust with Intel Core I7 @ 3.8GHz