Perilous Pier
2026-12-04 Original Prompt Part 1
Looks like we’re trying to tell a barcode printer how to reprint many damaged colorful barcodes, and we want to use the fewest instructions possible to do it.
Once we give the printer its instructions, they will repeat across the entire
barcode, so we’re essentially looking for the length of the shortest sequence of
colors we can repeat to build back the entire barcode. I would have reached
for itertools.batched()
here to split the barcode into equal-sized batches and check that they match,
but the ? wildcard character made that approach much clunkier than I wanted.
Thinking in terms of equal-sized batches did lead me to a useful insight,
though; I just had to shift my perspective a bit.
Let’s say we’re checking the barcode rygbrygbryg, and we want to verify
whether a list of 4 instructions would work. We can put these characters in a
4-column table like this, with each character going from left to right in each
row. Each row represents an equal-sized batch (with the last batch possibly cut
off), and each column represents a certain offset within that batch (offset 0
of each batch, offset 1 of each batch, etc.).
| Offset 0 | Offset 1 | Offset 2 | Offset 3 |
|---|---|---|---|
r | y | g | b |
r | y | g | b |
r | y | g |
If all rows of the table match each other (not counting the cut-off portion of
the last row), then this number of instructions would work for printing the
barcode; if they don’t, then it wouldn’t. And in this case, it’s easy to see
that all rows match, and thus 4 instructions would work for printing
rygbrygbryg — here, we could use the instructions rygb.
Equivalently, we could instead look at all the columns, and check whether all the colors within them match each other. After all, if all batches match, then so do all their first characters, and all their second characters, and so on. This is also easy to see in this case — arguably even easier — and it also confirms that 4 instructions would work here.
Now let’s say we’re checking the barcode ry??ryg?r??, also with 4 instructions
per batch. For this barcode, the table would look like this.
| Offset 0 | Offset 1 | Offset 2 | Offset 3 |
|---|---|---|---|
r | y | ? | ? |
r | y | g | ? |
r | ? | ? |
Just like before, we could check that all the rows match, or that all the
colors match within each column (where ? would match with any color, and can
be ignored). It may be harder to see on a row-by-row basis, so let’s check each
column’s contents in turn:
- Offset 0 has
r,r, andr. The only unique character isr, so everything in column 1 matches. - Offset 1 has
y,y, and?. The only unique characters areyand?— and?matches any color — so everything in column 2 matches. - Offset 2 has
?,g, and?. The only unique characters aregand?— and?matches any color — so everything in column 3 matches. - Offset 3 has
?and?. The only unique character is?— and?matches any color — so everything in column 4 matches.
And because we know that everything in all four columns matches, we know that
all batches must therefore match, and thus 4 instructions would work in this
case — here, one set of instructions we could use is rygb.
So to check whether a given size works as a number of printer instructions, we
can look at the columns at each possible offset from 0 to size-1 and check
that they have up to one unique character (not counting ?). As a matter of
fact, the “columns” are made of equally-spaced characters from the barcode, so
we can get each column by slicing
our barcode string — something like barcode[offset::size], to get every
sizeth character starting at offset. Then it’s only a matter of trying
bigger and bigger size values until one works.
def min_num_instructions(barcode: str) -> int: # NOTE For n instructions to work, every n colors in the barcode # should give up to one unique color (not counting "?"). Thus, we # can return the first size for which this is true at all offsets. for size in range(1, len(barcode) + 1): if all( len(set(barcode[offset::size]) - {"?"}) <= 1 for offset in range(size) ): return size assert False # The length of the barcode itself will always workNote
An easy way to get the unique values from an iterable is to put its items in a
set, which removes duplicate values. This is what I did here to get the unique
characters from each “column” (i.e. sliced barcode) — though because I want to
ignore the ? character, I also subtract the set {"?"} to remove that
character before I count how many unique characters I have.
We can do this to every barcode from the puzzle input, and add up all the
results with a simple sum() expression.
...
class Solution(StrSplitSolution): def part_1(self) -> int: return sum(min_num_instructions(line) for line in self.input)Part 2
Now instead of printing many small barcodes, we have to print one large
barcode made of all the small ones put together. The str.join()
method takes care of putting all the barcodes together, and we can in fact use
the same process from before on this large barcode to find the answer.
...
class Solution(StrSplitSolution): ... def part_2(self) -> int: return min_num_instructions("".join(self.input))Luckily, our approach is fast enough on its own that it finds the answer in no time flat!