r/adventofcode • u/not-the-the • 19h ago
Meme/Funny Reading [2026 Day 4 Part 2] in despair, until...
I somehow guessed exactly what part 2 would be while solving part 1, and thus avoided the naive O(6^n) solution for it, instead dealing every letter from a barcode into buckets like playing cards and checking all of them for homogeneity. Code finished in 2 seconds :)
92
Upvotes
55
u/benjymous 19h ago
Yeah, my part 2 was adding input.Replace("\n", "")
16
u/RazarTuk 19h ago
I think my solution is technically O(n2), although if you're naïvely going off number of nested loops, you'd mistake it for O(n3)
I just go: For each code length, from each position in the first repeat, step forward
sizecharacters at a time. Then at each step, I check if it's either a ? or matches the color I'm looking for, updating it if I'm looking for ? and find an actual color2
u/Roppano 16h ago edited 16h ago
reading the comments, I think I have more unique solution:
my idea is based on congruence. if an instruction has length of
i, then it only works for the barcode if at anyposition % ithere's only one kind of character (or?, ofc).you'll need at least as many instructions as there are distinct colors in the barcode, so I check that first. I put all the determined characters in buckets based on
position % i, and see if I get any clashes.bandggo into buckets 2 and 0, andvwould also like to go in bucket 2, but that's already occupied byb.So next, trying
i = 4, now the bucket looks likeso that's a correct, and minimal solution. Written in rust, part2 runs in 14ms
9
u/AlpacaDC 17h ago
I was absolutely dreading part 2 since it took a lot of time and brain power to solve part 1. Luckily I just had to change the input parsing.
14
u/Positive_Concert_774 19h ago
I'm truly puzzled what the heck would lead anyone to make an O(6^n) solution for this
3
u/Ok_Manufacturer_8213 16h ago
I feel soo dumb for not understanding it
3
u/QultrosSanhattan 14h ago
3
u/Danefantom 10h ago
Solution run time for part 1 ~ 60s.
Looking at part 2, "Uh oh.... Ah, I'm dumb" ~ 0.016s
5
u/_alba4k 18h ago
My solution is technically O(1) since the input is constant.
It takes around 1.34e3106 years to run, but that's no big deal right?
(I trie the naive appraoch first as the input was reasonably small and it ran part1 in ~10s. tried part2 for the lolz and it got OOM killed after a few minutes)
1
u/UnimportantMessages 16h ago
2s? My C# solution finished part 2 in 1ms runtime. Not sure you’ve landed on the right algorithm there buddy.
2
u/vegan_antitheist 18h ago
I brute forced the first part. It was easier starting from scratch than fixing what I had. I won't drop the first commit even though it's embarrassing if anyone ever sees it.
My solution now doesn't even use recursion. It's so simple.
1
u/wjholden 17h ago
Thanks so much for this - your analogy of playing cards helped me understand a fast algorithm to solve this puzzle!
1
u/PercussiveRussel 16h ago
I was being too clever for my own good and parsed the number as 6 bitmasks of known digits. Part 1 runs in 150 us with
u16s. After pulling in BigInt crate, my part 2 runs in 6 ms :(1
u/ConfectionUsed2828 13h ago
Are you me..?
1
u/XNeedNoName 12h ago edited 10h ago
i used bfs to solve part 1 (sadly cant check a graph with 3.5401 * 10^10385 nodes)
(edit: im so dumb i solved part with a solution i thought of and rejected within the first 2 mins of trying to solve part 1
1
u/atrocia6 11h ago
For me this was one of those days where the solution I used for part 1 was adaptable to part 2 with minimal modification; my Python solution for part 2 runs in a bit under .2s.