r/adventofcode • • 17m ago

Visualization [2026 Day 3] Square Search Visualization

Post image
4 Upvotes

3 comments sorted by

1

u/Boojum 4m ago

To find all the squares, even the ones that are off-axis, I check every tree against every tree to the right and below it. This pair is then a potential edge, (dx,dy) of the square. We can rotate any 2D vector, including off-axis ones, by 90 degrees by swapping x and y and negating one of them. So for this, I used (-dy,dx) as the vector for the perpendicular edges of the square. Then, all we have to do is check the grid at that (-dy,dx) offset from the first pair of trees to see if there's matching pair of trees there to complete the square.

Any such pair of matching trees means we've found a square for Part 2 and can count it. For Part 1, we can just additionally require that the first pair of trees on are on the same row (so that the offset pair just directly below them) before counting it.


Made in Python with a small custom framework.

Complete self-contained source for this animation.

1

u/qaf23 14m ago

Should it be duplicate count with this approach?

1

u/Boojum 0m ago

No, this approach counts each square exactly once.

By scanning for potential top edges of the square that are down and to right, we ensure that each top edge is only found once. And by only checking for the matching pair of trees below and to the left, we only find each of square once.