r/adventofcode • u/RazarTuk • 15h ago
Visualization [2026 Day 3] An alternative algorithm based on diagonals
First of all, credit to u/Boojum for making the visualization library I used. And second, pardon some of the issues. It crashes my computer if I try making a gif directly, and I can't figure out how to upload an mp4, so you're going to have to deal with artifacting from the converter.
That said, I actually used diagonals instead of sides. For part 1, this was just the natural option to me. It's easy to check whether two trees can be the opposite corners of a grid-aligned square, so I do that as a surface filter before checking the other two corners. And because I used diag[0] == diag[1] instead of abs(diag[0]) == abs(diag[1]), there's even a primary diagonal to avoid double counting.
For part 2, I changed the surface filter to the Manhattan length of the diagonal being even, which is logically equivalent to all four corners being on the grid. Then I used circumradius + rotated circumradius to generate and check the other two corners. The main catch is that there isn't an obvious check for a primary diagonal, so I wind up double counting everything, because I'm actually counting diagonals. But because it always double counts, I can easily just divide the answer by 2. (It isn't clear from the visualization, but I start by finding all the trees, so I can still do for (i in 0..<trees.size) for (j in (i+1)..<trees.size) to at least only count each diagonal once)
EDIT: Figured out a filter to avoid double counting. diag[0] * diag[1] > 0 || diag[1] == 0
3
u/RazarTuk 15h ago
Actually, looking at it graphically, I'm realizing that you can avoid double counting by just using the same search space as sides. Down and right or straight right. It just wasn't obvious to me, because I extracted all the trees into a list to make it easier to run "for each pair of trees" and only used the grid for checking whether there was a tree somewhere, as opposed to directly iterating over the grid for the pairs
4
u/thblt 15h ago
The issue with diagonals is going to be deduplication, isn’t it ? My side-based solution only considers pairs of points where the second point is below the first, and either exactly below or at its left. This guarantees I won’t get negative numbers when projecting the other two points, and each square will be considered once (of course you can have that restriction with diagonals)
9
u/deividragon 15h ago
My solution is very similar to this! And yeah, I didn’t see an obvious way to avoid the double counting, so I just divide the final count by 2. Nice visualization!
1
u/Boojum 9h ago edited 6h ago
Ah, I see what you meant about using the pairs of the trees as diagonals of potential square now!
If we take two trees, (x1,y1) and (x2,y2), as the opposite corners, we can find the mid point between them which will be the center of the square. Then we can just check +/- half the perpendicular to that diagonal in either direction.
Basically:
Or, fully expanded:
BTW, I'm glad my initial code was helpful. FWIW, I do all my encodings with:
3
u/custardgod 13h ago
I tried doing this solution, but it didn't quite work out. Gave up on part 2 but maybe I'll come back to it at some point
1
u/Chroiche 7h ago
This is the approach I landed on, but I only checked diagonals with positive or infinite slope.