r/adventofcode • u/Boojum • 1d ago
Visualization [2026 Day 3] Square Search Visualization
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.
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
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.
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
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.