Metadata
Date
Tagged
Part of series
Older post
Advent of Code 2026 Day 4
Day 4: Perilous Pier
https://adventofcode.com/2026/day/4
CHOO, CHOO! The train arrives near a bunch of shipping containers.
Each container has a barcode. Unfortunately, they are damaged and only partially readable.
Todays input is a list of barcodes.
An example input looks like this:
rgbrgbrgbr?rvo??v???y??bg?v????b????????Each line represents a single barcode.
Barcodes consist of an ordered list of any of six colours: red (r), orange (o), yellow (y), green (g), blue (b), and violet (v).
Unreadable characters are replaced by a ?.
Part 1
The printer repeats a list of instructions (colours) until it reaches the barcode length, cutting off any extra characters at the end.
For example if you give it rgb, the printer will repeat it until the barcode is complete.
A resulting barcode of length 10 will be rgbrgbrgbr.
We want the shortest instruction list that matches every known colour in a barcode.
The question asks for the total number of required instructions.
Helpers
I made a helper that figures out that minimum for 1 barcode. It is passed to my helper as a string, and I turn it into a list of characters.
Then I try every possible pattern_len from 1 up to chars.len() - 1, picking the first one that fits.
If none of those shorter lengths work, I fall back to the length of the entire barcode.
To check if a pattern_len fits, I inspect if the character at every index of the pattern can be repeated safely into the full length barcode.
Every character at index, index + pattern_len, index + 2 * pattern_len, and so on must use the same colour.
A ? can take on any colour, so I skip it.
The first non-? colour in that slot becomes the colour every repetition position must match.
If a non-matching colour appears in the repetition sequence next, the pattern_len doesn’t fit.
fn min_instructions(barcode: &str) -> usize { let chars: Vec<char> = barcode.chars().collect();
for pattern_len in 1..chars.len() { if fits_pattern(&chars, pattern_len) { return pattern_len; } }
chars.len()}
fn fits_pattern(chars: &[char], pattern_len: usize) -> bool { for index in 0..pattern_len { let mut expected = None;
for &ch in chars[index..].iter().step_by(pattern_len) { if ch == '?' { continue; }
match expected { None => expected = Some(ch), Some(colour) if colour != ch => return false, Some(_) => {} } } }
true}After figuring out how to solve a single code, loop through the whole list of codes and sum those results.
pub fn part_1(input: &str) -> usize { input.lines().map(min_instructions).sum()}Part 2
Turns out the whole input was meant to be one giant barcode. Ignore those newlines!
pub fn part_2(input: &str) -> usize { let barcode: String = input.chars().filter(|&c| c != '\n').collect(); min_instructions(&barcode)}Final code
fn min_instructions(barcode: &str) -> usize { let chars: Vec<char> = barcode.chars().collect();
for pattern_len in 1..chars.len() { if fits_pattern(&chars, pattern_len) { return pattern_len; } }
chars.len()}
fn fits_pattern(chars: &[char], pattern_len: usize) -> bool { for index in 0..pattern_len { let mut expected = None;
for &ch in chars[index..].iter().step_by(pattern_len) { if ch == '?' { continue; }
match expected { None => expected = Some(ch), Some(colour) if colour != ch => return false, Some(_) => {} } } }
true}
pub fn part_1(input: &str) -> usize { input.lines().map(min_instructions).sum()}
pub fn part_2(input: &str) -> usize { let barcode: String = input.chars().filter(|&c| c != '\n').collect(); min_instructions(&barcode)}