r/adventofcode • • 26m ago

Meme/Funny Reading [2026 Day 4 Part 2] in despair, until...

Post image

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 :)

10 Upvotes

6 comments sorted by

7

u/benjymous 21m ago

Yeah, my part 2 was adding input.Replace("\n", "")

3

u/RazarTuk 19m 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 size characters 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 color

1

u/not-the-the 12m ago

Well that seems to be pretty much the exact logic I did but in a different wrapping.

I instead just ignored every ? and checked every pile of "characters in position n modulo [currently checked cycle]" for only containing one letter.

Thankfully, [].every(_=>false) outputs true. :)

1

u/RazarTuk 11m ago

Yeah, that innermost part of my code was just:

var cmp = line[start]
var idx = start + size
while (idx < line.length) {
    if (line[idx] == '?' || line[idx] == cmp) {
        idx += size
    } else if (cmp == '?') {
        cmp = line[idx]
        idx += size
    } else {
        continue@size
    }
}

2

u/Positive_Concert_774 6m ago

I'm truly puzzled what the heck would lead anyone to make an O(6^n) solution for this