r/adventofcode • u/Alpha_wolf_80 • 8h ago
Meme/Funny [2026 Day 04 # (Part 2)] The ultimate plot twist
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
33
u/XLNBot 7h ago
L is O(n) so your solution is O( n2 )
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
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