r/adventofcode • • 8h ago

Meme/Funny [2026 Day 04 # (Part 2)] The ultimate plot twist

Post image

My final part two ended up being O(L * n) where L is the answer and n is the barcode length. I couldn't even believe my eyes when it happened. If they had asked for the number of possible valid instructions, I would have gotten an O(L * n) algo, which is quite surprising! This one was really fun!

Code for part 1 and 2: Advent-Calendar-Python/2026/4.py

15 Upvotes

11 comments sorted by

33

u/XLNBot 7h ago

L is O(n) so your solution is O( n2 )

15

u/XLNBot 7h ago

After looking at your solution and ignoring tweaks and optimizations, it seems to me that you're also just trying every possible length like everyone else?

4

u/NullOfSpace 5h ago

“Brute force” in this context I think refers to generating all possible combinations of 1,2,3,4,… colors and checking if they work. Whether or not anyone actually did that is another question entirely.

1

u/paparabba 3h ago

I did that for part 1… before realising it’s gonna take forever for part 2 and then refactored for the O(n2) soln

12

u/Positive_Concert_774 6h ago

The absolute best you could do is O(NlogN) so no, you didn't - more specifically, there is another N hidden in your L. So you're wrong, but at least you shouted it off the rooftops, so no biggie ;)

7

u/Mmlh1 7h ago

My plot twist was that not only did my code work for part 2 immediately, but part 2 has half the runtime of part 1. Was not expecting that one.

11

u/p88h 7h ago

Ugh, the plot twist is 'brute forcing' is what your algorithm is doing.

The second plot twist is that it runs in O(l+n), rather than O(l*n) on the class of inputs that are actually there

8

u/ben-guin 7h ago

Some people have mentioned solutions that are O(6n) (or possibly worse) which I think are more in line with the idea of "brute force" for this problem

4

u/daggerdragon 6h ago

During an active Advent of Code season, solutions belong in the Solution Megathreads. In the future, post your solutions to the appropriate solution megathread.

1

u/AtmosphereArtistic61 7h ago

Best is probably O(n log n) via some FFT magic.

1

u/StickyDeltaStrike 6h ago

Omg I forgot about advent of code