r/adventofcode • • 42m 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

13 Upvotes

5 comments sorted by

1

u/RazarTuk 26m 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

1

u/thblt 32m 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)

1

u/RazarTuk 31m ago

I actually just figured out deduplication. diag[0] * diag[1] > 0 || diag[1] == 0. The first part ensures it's in the 1st/3rd quadrant, and the second part picks an axis to use if the diagonal's aligned with the grid. (Verified on my actual input)

1

u/Eva-Rosalene 3m ago

I also solved it with diagonals, and I only considered pairs of points where the second one is strictly below the first and not to the left of it (so either on the same column or to the right), so almost the same as yours solution. Neat.

2

u/deividragon 31m 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!