Files
aoc/2026/src/day4.rs
T
2026-12-04 00:15:01 -07:00

151 lines
3.8 KiB
Rust

use std::collections::VecDeque;
use aoc_runner_derive::{aoc, aoc_generator};
use itertools::Itertools;
#[aoc_generator(day4, part1)]
fn parse(input: &str) -> Vec<Vec<u8>> {
input.lines().map(|l| l.as_bytes().to_owned()).collect()
}
const OPTIONS: &[u8] = b"roygbv";
fn test_instructions(barcode: &[u8], instructions: &[u8]) -> bool {
instructions
.iter()
.cycle()
.zip(barcode.iter())
.all(|(inst, code)| *code == b'?' || code == inst)
}
fn possible_insts(code: u8) -> Vec<u8> {
if code == b'?' { OPTIONS.to_owned() } else { vec![code] }
}
fn shortest_sequence(barcode: &[u8]) -> Vec<u8> {
// use bfs to find the lowest cost matching instruction set
//
// nodes - instruction sequences, edges cost 1, return on finding a sequence
// where test_instructions passes
let mut q = VecDeque::new();
q.extend(possible_insts(barcode[0]).into_iter().map(|c| (1, vec![c])));
while let Some((cost, next)) = q.pop_front() {
if test_instructions(barcode, &next) {
return next;
}
q.extend(possible_insts(barcode[next.len()]).into_iter().map(|c| {
let mut new: Vec<u8> = next.clone();
new.push(c);
(cost + 1, new)
}))
}
unreachable!("no solution found")
}
fn shortest_sequence_len(barcode: &[u8]) -> u64 {
#[derive(Copy, Clone, PartialEq)]
enum Instruction {
Any,
Colour(u8),
}
// Start with the shortest sequence, build it from the barcode, placing fixed barcodes for unknown instructions, skipping
// when there is no constraint, and moving on to the next length if there is a clash
'outer: for len in 1..=barcode.len() {
let mut instructions = Vec::from_iter(std::iter::repeat_n(Instruction::Any, len));
for (i, c) in barcode.iter().enumerate() {
if *c == b'?' {
continue;
} else if instructions[i % len] == Instruction::Any {
instructions[i % len] = Instruction::Colour(*c)
} else if Instruction::Colour(*c) == instructions[i % len] {
continue;
} else {
//clash so move on to the next length
continue 'outer;
}
}
return instructions.len() as u64;
}
unreachable!("no solution found")
}
#[aoc(day4, part1, BFS)]
fn part1(input: &[Vec<u8>]) -> u64 {
input
.iter()
.map(|barcode| shortest_sequence(barcode).len())
.sum::<usize>() as u64
}
#[aoc(day4, part1, Builder)]
fn part1_builder(input: &[Vec<u8>]) -> u64 {
input.iter().map(|barcode| shortest_sequence_len(barcode)).sum()
}
#[aoc(day4, part2, Builder)]
fn part2(input: &str) -> u64 {
let barcode = input.as_bytes().iter().filter(|c| **c != b'\n').copied().collect_vec();
shortest_sequence_len(&barcode)
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
const EXAMPLE: &str = "rgbrgbrgbr
?rv
o??v???y
??bg?v??
??b?????
???";
#[rstest]
#[case(b"rgbrgbrgbr", b"rgb")]
#[case(b"?rv", b"vr")]
#[case(b"o??v???y", b"ovyvo")]
#[case(
b"rro???g?g?????r???????g???yy?v?o???vb?g???y??v?or????r?o????????r??v?r",
b"rrovbrgogryybvro"
)]
fn inst_check(#[case] barcode: &[u8], #[case] instructions: &[u8]) {
assert!(test_instructions(barcode, instructions))
}
#[rstest]
#[case(EXAMPLE, 16)]
fn part1_example(
#[case] input: &str,
#[case] expected: u64,
#[values(part1, part1_builder)] version: fn(&[Vec<u8>]) -> u64,
) {
assert_eq!(version(&parse(input)), expected);
}
#[rstest]
#[case(
"rro??
?g
?g???
??r?????
??g?
??
yy?v?o
???v
b?g??
?y??v?or
????r?o
??
?????
?r??v?r",
16
)]
fn part2_example(#[case] input: &str, #[case] expected: u64) {
assert_eq!(part2(input), expected);
}
}