Part 1

This is pretty straightforward. You can compress the instructions only if all of the characters that share the same modulus are the same (including wildcard ‘?’s). So you just go in order: can I satisfy this with 1 instruction, 2 instructions, 3 instructions and so on. You’re guaranteed to terminate at \(n\) instructions and each check is pretty cheap.

function find_shortest(seq)
    n = length(seq)
    for len = 1:n
        viable = true
        for jj = 1:len
            char = seq[jj]
            for kk = (jj+len):len:n
                (seq[kk] == char || seq[kk] == '?') && continue
                if char == '?'
                    char = seq[kk]
                else
                    viable = false
                    break
                end
            end
            viable || break
        end
        viable && return len
    end
end

function part1(fname = "input.txt")
    return sum(find_shortest(line) for line in readlines(fname))
end

Part 2

This one didn’t require any changes to the algorithm. I tend to like these sorts of problems where you massively scale up the input and thus need to be efficient. It’s fun when the scalable solution isn’t the obvious solution. But here the obvious solution is also the scalable solution?

function part2(fname = "input.txt")
    return find_shortest(join(readlines(fname), ""))
end

Something Extra

Can we compress the compression? So instead of having the elves build the instructions to print the barcodes, let’s have them build instructions to print the instructions that print the barcodes! Or go even one step further to build the instructions to print the instructions that print the instructions that print the barcodes.

For instance, let’s consider the starting sequence “rgbrrgbrrgb”. Clearly we can build this with the instructions “rgbr”. But we can construct that with “rgb”, which we cannot compress any further: this sequence is itself incompressible.

There should always be an end to this! Because each meta-layer of useful instructions is smaller than the preceding layer, we eventually hit one of these incompressible sequences and stop.

To solve this we just need to modify our code from above to:

function explicitly_find_shortest(seq)
    n = length(seq)
    instruction = Char[]
    for len = 1:n
        empty!(instruction)
        viable = true
        for jj = 1:len
            char = seq[jj]
            for kk = (jj+len):len:n
                (seq[kk] == char || seq[kk] == '?') && continue
                if char == '?'
                    char = seq[kk]
                else
                    viable = false
                    break
                end
            end
            viable || break
            push!(instruction, char)
        end
        viable && return String(instruction)
    end
end

function recursive_compress(seq)
    layers = [seq]
    while true
        next_seq = explicitly_find_shortest(layers[end])
        length(next_seq) == length(layers[end]) && break
        push!(layers, next_seq)
    end
    return layers
end

We apply it to our Part 2 input and get:

Layer Length Wildcards
0 20000 13331
1 1597 11

Disappointing. We can’t compress our final solution any further. Maybe we’ll have more luck looking at all of our Part 1 lines.

Depth Lines Mean initial length Mean final length
0 184 4.03 4.03
1 3389 4.79 1.64
2 416 6.69 2.59
3 31 7.45 2.45
4 2 8.00 3.00

Ok! That’s more like it. Most can only compress a little but now we have more which start compressing further. Notably the longer ones (shorter can’t go too deep because you just run out of letters).

Let’s take a look at the two longest chains:

Line Compression chain Lengths
1208 ?vg?b?vb → bvg?b?v → bvg?b → bvg? → bvg 8 → 7 → 5 → 4 → 3
3282 ?go?yyy? → ygo?yy → ygo?y → ygo? → ygo 8 → 6 → 5 → 4 → 3

They had some strategically placed wildcards, which helped us out.

One would think that the longer a string is, the more likely these sorts of things are to happen. Let’s consider randomly sampling from a uniform distribution over strings to try and tease out whether we got unlucky with the original prompt or whether there’s something else going on. We’ll plot the expected number of layers and sweep over varying fractions of wildcards while varying the length.

At low wildcard rates string length barely matters: we rarely succeed compressing converging around mean layers of ~0.25. But as we get more wildcards we start seeing a divergence with longer sequences having a higher rate. But this reaches diminishing returns: once we start getting too many wildcards we start to collapse back down: my interpretation is that we can compress too well early on and thus don’t have enough left for further compression. Indeed at q=1 you only compress a single time: down to a single instruction.

Look at this it’s sort of weird that our original input didn’t compress further! Like slightly under 2/3 of my input is wildcards which corresponds to a 91.9% chance of being able to go at least two layers deep. I’ll have to see if others’ inputs compressed further and I just got a low probability draw or whether there’s probably something in how the inputs are generated that results in this.