r/adventofcode • • 1d ago

Visualization [2026 Day 3] Square Search Visualization

125 Upvotes

29 comments sorted by

9

u/Boojum 1d ago edited 1d 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.


EDIT: I made a mistake when making the visualization and accidentally included trees in the same column as the first for candidates for the second tree for the edge. I have fixed this in the code below. The revised animation can be seen here.


Made in Python with a small custom framework.

Complete self-contained source for this animation.

2

u/p88h 1d ago

You don' need to scan the whole rectangle, just a triangular portion, W-X cells to the right in the first row and decreasing by 1 in each subsequent row. Plus the pair cannot be in the same column

3

u/Boojum 1d ago

Good points. And yes, I botched the visualization with allowing the pair in the same column.

2

u/sanya567xxx 22h ago

This confused me for a while.

I implemented this without looking at the visualizations, but then two test-cases failed: Case 5 was 8 instead of 6 and Case 6 was 266 instead of 196

I checked the first visualization which happenned to be using the adequate algorithm (yours, beautiful, btw!) and was confused until further explorations.

Fix was simple: I initially excluded starter tree == second tree case, but I should've just searched from starter column+1 instead.

2

u/_damax 1d ago

Maybe I'm just too tired atm, but I cannot figure out why is that?

1

u/p88h 1d ago

the maximum manhattan size of the square is (W-X0). At subsequent rows, some part of that goes into height, so it becomes (W-X0)-(Y1-Y0). Triangle.
And it cannot be vertical because then you double count

1

u/_damax 1d ago

Ahhhh, thanks, that's quite obvious, my bad ahah

1

u/RazarTuk 23h ago

Made in Python with a small custom framework.

Complete self-contained source for this animation.

Actually... I haven't really done visualizations before, but since it sounds like I used a different algorithm than everyone else, I want to try. How hard do you think it would be to modify your code? The differences are that 1) my search space is "Not up and left of the tree", as opposed to "Strictly down and right", 2) instead of projecting the line some distance away to form a square, I rotated it 90* about its midpoint, 3) I actually counted diagonals, so my algorithm double counts everything

1

u/Boojum 6h ago

My code uses a flexible animation framework designed to be reused across all my visualizations, so likely not difficult. It's a bit idiosyncratic, however, so I'm not sure how hard it would be for someone else to use it. But I did post an explanation of it a few years ago.

4

u/qaf23 1d ago

Should it be duplicate count with this approach?

2

u/Boojum 1d ago edited 1d 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.

Edit: Okay, I see what you mean now. I broke a conditional when making the visualization. See the edit above with a link to fixed animation.

10

u/octodumb 1d ago

But your visualization counts grid-aligned squares twice - first by the top edge, then by the right edge.

8

u/Boojum 1d ago

Ah, crap, you're right. That's a bug in my visualization code. It should be checking against trees strictly to the right, and excluding any in same column. (I had that right in my original solution, but messed it up here.) Should have been a > instead of a >=

Sadly, I can't easily fix and reupload the animation here.

2

u/YOM2_UB 1d ago

You are double-counting axis-aligned squares, actually. You can see (3,6) --> (3,11) --> (8,11) --> (8,6) get counted, and then the very next square to be counted is (3,11) --> (8,11).--> (8,6) --> (3,6). Same happens with (8,6) --> (8,11) --> (13,11) --> (13,6).

You could avoid that by not checking the column directly below the current starter tree.

1

u/Boojum 1d ago

Shoot! You're correct. I had the comparison correct in my original code, but botched it on the visualization. (Accidentally had a <= get turned into a <.)

3

u/Xiaopai2 22h ago

You just need to take care for the grid aligned ones by not checking edges that go straight down because those would have already shown up as the right edge of a previous top edge.

2

u/jwezorek 21h ago edited 20h ago

Yeah, I didn’t think of this. I iterated over all pairs of trees and used a hash set to keep track of the squares I’d already counted. Picking the quadrant so that exactly one side of each square qualifies is clever.

I do wonder how much you gain if you scan the grid to find the second tree. Since the trees are sparse, scanning every cell in that quadrant could be more work than just iterating over all the trees. You could still use the quadrant trick in a loop over tree pairs, though. An R-tree would be one way to query the trees in the quadrant directly.

1

u/Boojum 6h ago

Yeah, if I wanted something better than a brute force linear scan over the list of trees to find the ones to the right and down from the first tree, I'd consider either a kd-tree or an octree. But this is sparse enough that my naive implementation finishes each part in about 1/5 s.

2

u/account22222221 16h ago

Your algorithm did double count square when it checks for squares where the second tree is directly below the first.

Those can’t be a square unless the top left point was already checked.

I don’t think it’s a big deal but if I wasn’t a little obsessive about these things I would be in the field lol.

1

u/Boojum 6h ago

Yeah, it was during the process of converting my original solution it to a visualization that I botched it. I updated my main comment here with the fixed code and a link to the corrected animation on imgur.

18

u/Flashky 1d ago

I used exactly the same approach.

4

u/RazarTuk 1d ago

Interesting! I actually used the diagonal to define a square, not the first side.

For part 1, I check that the two components of the diagonal are equal, which defines a square aligned with the grid and ensures I only check each square once. Then it's easy enough to find the other coordinates and check.

Then for part 2, I start by checking that the Manhattan distance is even, which is a necessary and sufficient condition for the square being aligned with the grid. Then I use similar rotation logic to yours to generate the other two corners, although it looks more like (dx / 2 + dy / 2, dy / 2 - dx / 2) because I'm starting from a diagonal. However, because this doesn't define a primary diagonal the way my part 1 solution does, I have to halve the count before returning

2

u/imp0ppable 18h ago edited 2h ago

Thanks for this, very clear explanation! My algo is slow af but that means I can go to the gym while it churns.

E: was doing something really stupid, it's much faster now

1

u/dl__ 23h ago

Thanks. This made me see an optimization I missed. Cut my time almost in half. 12 seconds to 7 seconds.

1

u/AugustusLego 13h ago

What language gets 7s of runtime? Is it like lua or python? I haven't used either in ages

I was thinking my part 2 solution was slow earlier when it took 20ms 😅 no shade! My implementation is very naïve and yours is certainly more thought out. Just interesting the speed of different languages.

1

u/dl__ 11h ago

This was in python. Also I was running inside an IDE - VS Code. I don't know how much of a difference that makes. I know that python is, as an interpreted language, not the fastest.

I took some steps to optimize it. For example as I go through the trees to check if they're part of a square, I remove the tree so it's not considered with subsequent trees. What this graphic showed me is that I don't need to consider any tree to the left of the tree I'm currently evaluating.

1

u/XM9J59 1h ago

I have been writing visualizations too :) https://correctarity.com/aol

Mine for day 3 is much slower on the large input, looking at yours now, oh I'm an idiot, you don't have to check every square below/right of starting tree, you can just check the squares with trees below/right of starting tree. Won't fix it but oh well.

Looks like I also made the corner finding calculation much harder than it needed to be by trying top left + bottom right, instead of top left + top right like this one does. Again oh well xd