Advent of Code 2026 Day 4: Perilous Pier
from advent import parse
codes = parse(4)
def is_cycle(n, code):
return all(
len({c for i in range(offset, len(code), n) if (c := code[i]) != '?'}) < 2
for offset in range(n)
)
def minimum_cycle_length(code):
return next(n for n in range(1, len(code) + 1) if is_cycle(n, code))
def solve(codes):
return sum(minimum_cycle_length(code) for code in codes)
assert solve(codes) == 7893 # Part 1
assert solve([''.join(codes)]) == 1995 # Part 2
Problem
We're given a list of barcodes containing either letters or a ? wildcard which can represent any letter. The primary goal is to identify the length of the smallest pattern that can be repeated to form the barcode. For example, the barcode rg?v??o??g?v? could be represented by repeating the 4-character pattern rgov 3.25 times.
Input Parsing
codes = parse(4) provides the following:
----------------------------------------------------------------------------------------------------
day04.txt ➜ 23977 chars, 3978 lines; first 3 lines:
----------------------------------------------------------------------------------------------------
?????
????oo??
?v??
----------------------------------------------------------------------------------------------------
parse(4) ➜ 3978 entries:
----------------------------------------------------------------------------------------------------
('?????', '????oo??', '?v??', 'r???g?', '??', '?v????', '?o?????', 'g? ... ???????', '??????', '?v')
----------------------------------------------------------------------------------------------------
Solution
The main part of the solution is the is_cycle(n, code) predicate function. It returns a boolean indicating whether the code can be represented by a cycle of length n. The first step is to collect all the letters (ignoring '?' since it matches anything) at the same pattern offset into a set using a set comprehension. The third argument of range() is the step size, which is the cycle length. For example, if we're determining whether a 3-char repeated pattern is valid, we first gather all the non-? characters at indices 0, 3, 6, etc. Then repeat for the second and third characters of the pattern.
If the len() of this set is less than 2, then that offset is valid. We'll do this for each offset in the pattern. For a 3-character pattern, we'll check offsets of 0, 1 & 2:
The all() function returns True if all of the elements are True which indicates all offsets of the pattern are valid:
all(
len({c for i in range(offset, len(code), n) if (c := code[i]) != '?'}) < 2
for offset in range(n)
)
The next helper function, minimum_cycle_length() simply tries all possible cycle lengths, starting at 1, and returns the length of the first valid one. One optimization would be to start at the number of unique letters in the code, but that complicates the code, and doesn't really buy much:
def minimum_cycle_length(code):
return next(n for n in range(1, len(code) + 1) if is_cycle(n, code))
With that in place, the solve() function simply sums up the minimum cycle lengths for all of the codes. The only difference for part two is to combine all of the codes into a single code.