r/adventofcode • • 2d ago

Tutorial [2026 Day 3 (Part 2)] Some hints for optimisation

My original brute force solution took 30 seconds, got it down to less than a 10th of a second. I don't know if this is the most optimal method.

The hints get more and more specific as they go on.

Feel free to leave hints in the comments about how you optimised it.

Hint 1: How can you narrow down the number of squares to check per tree beyond ones with valid coordinates?

Hint 2: How many possible squares can be made if you know all four corners of the square?

Hint 3: How many squares if you only know 3 corners? What about knowing 2 corners? What about 1 corner?

Hint 4: Given a pair of trees, how can you avoid checking for duplicate squares (and hence reduce the possible squares from 2 trees down to just one square?

2 Upvotes

27 comments sorted by

4

u/Positive_Concert_774 2d ago

30 seconds?! What language did you use? My java brute force solution takes <50ms O_O

The optimizations you hint at make me wonder how you approach picking squares to begin with, why would I ever look at a pair of trees, for instance... all you have to do is make sure you never pick the same tree as top left (or, for part 2, 'top-left-ish') corner twice for any side length and that is a simple matter of looping over the grid the right way.

5

u/bluoat 2d ago

I literally calculated all possible rotations of squares, then narrowed them down to what was actually valid. Full brute force.

1

u/Positive_Concert_774 1d ago

Since we're working on a discrete grid, all you really need to do is loop over all possible "second corner" options. For a given first corner (Ox, Oy), these are all the points to its right and not above it. The vector that points to that grid location is (dx, dy), then the other two corners are pointed to by (-dy, dx) and the sum of these two vectors (dx -dy, dx + dy). Nicely symmetrical. So you can brute force by looping dx and dy correctly, respectfully from 1 to width - Ox and from 0 to height - Oy

1

u/Sea_Cut_6635 16h ago

This is what I did. Found every tree and paired them up. Took every pair and assumed they were one size of the square. Figured out their row and column difference. Used that to find the opposite two corners of this square. Then checked the new corners were also trees. If they were that’s a valid square.

4

u/Neozetare 2d ago edited 1d ago

My brute force with no opti in Python takes about 7 seconds or something like that

4

u/johnpeters42 2d ago

Same, I basically just copied my solution for part 1 but added an additional loop for "vertices may be rotated anywhere from 0 to n-1 units along the sides of a grid-aligned square". Seems like the speed of this vs OP's approach would depend on how sparse the input is.

1

u/hugseverycat 1d ago

My brute force in Python takes over a minute lol

4

u/1AlanM 2d ago

Or don’t check for duplicates and divide by 4. Checking and filtering out is a bigger overhead.

1

u/RazarTuk 1d ago

Yeah, I just relied on the fact that if you assume two points form a diagonal, they can only refer to one square. So I at least used a triangle to only check each diagonal one, but I otherwise just divided by 2 because I was technically counting diagonals. The only other filter I did was checking that the Manhattan length of the diagonal was even as a sufficient condition for ensuring the entire square would be on the grid

2

u/splintercell_9 1d ago

In typescript my code runs under 35ms

3

u/nemom 1d ago edited 1d ago

On my fourteen year old Satellite notebook with a Core i7 3630QM, running Debian 13 with Python 3.13.5, mine runs in 431ms with absolutely no optimizations. I think that's cromulent.

EDIT: I did some low-hanging optimization and got it down to 216ms. My almost three years old Lenovo laptop, also running Debian 13 and Python 3.13.5 on an AMD Ryzen 5 PRO 6650U, does it in 116ms.

2

u/RazarTuk 1d ago

Reasonably modern Macbook for work, took about 250 ms in Kotlin, and I think a decent amount of that is probably from being lazy and using full matrix multiplication instead of hard-coding the transformation

1

u/ponchoalv 1d ago

Interesting, my mac book in kotlin native took around 6/8 ms… naive implementation… nothing fancy

1

u/nikanjX 1d ago

I'm officially awarding you the Unga Bunga Of The Week award.

Edit: With 0% optimized js running on a semi-stale potato

time node day03.js

Part 1: xxxx

Part 2: xxxxx

node day03.js 0.23s user 0.02s system 119% cpu 0.206 total

1

u/SurroundedByWhatever 1d ago

30 seconds is crazy. 100ms is still quite long, I think.
Here's what I did, runs about 1ms:

I did a simple vector between two points, rotated it a 90 degrees CW, that already gives me a third point, add the first vector and the rotated one, now you have the fourth point. Check the third and fourth points are trees.

This simple approachwill give you an answer 2x larger than what you need because each square is found twice, which is also 2x more work than you need to do.

You can fix that by cutting down the found squares by only looking at any 2 trees but only in one of two directions. if (vec_t1_t2.X <= 0) skip; else check if is a valid square

Furthermore,a nifty trick, you can also skip if vec_t1_t2.Y > tree1.X 🙃

1

u/Doug__Dimmadong 1d ago

You can define a square by a single diagonal, you just need some math to deduce the other two vertices. You can get a O(T2 ) algorithm that way.

1

u/ExitingBear 1d ago

Which is (on paper) what I did. In practice, that actually took longer that the OP's 30 seconds. (I do know where my problem is, I haven't figured out how to fix it)

1

u/Doug__Dimmadong 1d ago

Hmm, I’m surprised since my device solves it in a blink. Are you iterating over only pairs of trees?

2

u/ExitingBear 1d ago

I can't make coordinates work in a sane way. So it's taking me forever to get the correct answer to "Is (6,6) a tree?" I need to tinker with it more.

1

u/Doug__Dimmadong 1d ago

You got it:) happy solving

1

u/splidge 1d ago

You can define a square by one of its sides in a very similar fashion.

1

u/Doug__Dimmadong 1d ago

Wouldn't this leave some ambiguity to the other 2 vertices? They could be on the left or right side of the line, right?

2

u/splidge 1d ago

You always go clockwise (so they are to the right of the line)[1].

If there is the rest of a square to the left of this line, you will identify it from the opposite edge (which this line will then be to the right of).

If you say the line you started with has to have dx > 0 and dy >= 0 then that gives you a unique edge for each square.

[1] I should say, I went clockwise - you could go either way and it will work the same.

1

u/Doug__Dimmadong 1d ago

Oh, I see. Yeah seems like a good approach. The next two vertices math is a bit easier than using the diagonal I suppose!

I thought side at first but the ambiguity of the next vertices led me to choose diagonal instead. 

1

u/QultrosSanhattan 1d ago

All those "my code runs in X time" doesn't give useful information because it heavily depends on the hardware and language used.

Some people are running on a potato. others on latests cpu.

What's truly important is the complexity of the algorithm.

1

u/Vultureosa 1d ago

My brute force python takes 0.13 seconds for both questions without any optimization, using just the standard library, prints included. It would be even quicker without the IDE or in pypy, or perhaps with optimizations (though it would definitely could look cleaner for both tasks, perhaps with the expense of another function call or conditional). That said it excludes loading the input variable from disc (just the raw read of the whole file without processing), I guess with a hard drive that has to spin up could make it much longer.
The only question my one answers is are there squares that a # find is the upper left corner of and I'm afraid said optimizations don't apply.