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

63 Upvotes

15 comments sorted by

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

1

u/Ok-Revenue-3059 14h ago edited 12h ago

I think I also did very close to this. I treat the first picked tree as "top left" point and the second picked tree as "top right" point. Then you can avoid double counting by restricting the second point search area so that top_right.x > top_left.x and top_right.y >= top_left.y

2

u/RazarTuk 13h ago

Yeah, the main unique part is that I feel like a lot of people treated the pair as a side of the square, while I treated it as a diagonal, primarily because my mind actually did jump to diagonals for part 1

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)

2

u/RazarTuk 15h 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)

4

u/Eva-Rosalene 14h 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.

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!

5

u/juhotuho10 11h ago

if you check only the second squares that are to the right of your first square, it nicely sidesteps the double counting and makes it quite a bit faster too

1

u/fnordargle 7h ago

This is where the choice of data structure to store the tree info helps.

I read in the map line by line, but I store the locations of the trees in two places in two different data structures:

  • A hash based on (x,y) coordinates so I can do an O(1) lookup for any given coordinate to see if it is a tree
  • As an array of arrays rows/columns of the tree locations. The "outer" array is an array per row with the inner array being the row number and the column numbers of any trees.

An example might explain it better:

.#... -> [0, 1] means (row 0, tree in column 1)
..#.# -> [1, 2, 4] means (row 1, trees in columns 2 and 4)
..... -> no entry
#.#.# -> [3, 0, 2, 4] means (row 3, trees in column 0, 2 and 4)
...#. -> [4, 3] means (row 4, tree in column 3)

I'll leave it as an exercise for the reader to understand why this helps...

1

u/Chroiche 7h ago

Just skip any pairs with 0 or negative slope between them

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:

mx, my = (x1 + x2)/2, (y1 + y2)/2   # Mid point
dx, dy = x2 - x1, y2 - y1           # Diagonal
px, py = -dy/2, dx/2                # Perpindicular to diagonal, halved
x3, y3 = mx + px, my + py           # Other corner in one direction
x4, y4 = mx - px, my - py           # Other corner in other direction

Or, fully expanded:

x3 = (x1 + x2 + y1 - y2)/2
y3 = (y1 + y2 + x2 - x1)/2
x4 = (x1 + x2 + y2 - y1)/2
y4 = (y1 + y2 + x1 - x2)/2

BTW, I'm glad my initial code was helpful. FWIW, I do all my encodings with:

ffmpeg -i frame%04d.png -vf palettegen -frames:v 1 -update 1 -y palette.png
ffmpeg -r 50 -i frame%04d.png -i palette.png -filter_complex "paletteuse=dither=none" -y 2026day04.gif

1

u/RazarTuk 7h ago

Yeah, for part 1, the diagonals felt obvious to me because it's an easy surface filter for "can these two points even be on a square?". Then for part 2, I did midpoint + perpendicular to generate the other two corners. Although because my custom grid library auto-truncates if you pass it a double as a coordinate, I needed that even Manhattan distance check to ensure there wouldn't be silent rounding errors

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/PhysPhD 10h ago

I'm in the same boat. I used diagonals for part 1 using tuples in Python, but couldn't figure out how to then convert that to a rotated square.

Looking at other solutions it seems the answer is to use complex numbers, but I've moved on now, so missing out on that star.

1

u/Chroiche 7h ago

This is the approach I landed on, but I only checked diagonals with positive or infinite slope.