diff --git a/2026/src/day4.rs b/2026/src/day4.rs index 5720e5f..724213a 100644 --- a/2026/src/day4.rs +++ b/2026/src/day4.rs @@ -1,7 +1,6 @@ -use std::collections::{HashMap, VecDeque}; +use std::collections::VecDeque; use aoc_runner_derive::{aoc, aoc_generator}; -use cached::proc_macro::cached; use itertools::Itertools; #[aoc_generator(day4, part1)] @@ -51,10 +50,11 @@ fn shortest_sequence_len(barcode: &[u8]) -> u64 { enum Instruction { Any, Colour(u8), - }; - // Start with the shortest sequence, build it from the barcode, continue building until a fixed instruction clashes with a fixed barcode + } + // 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(Instruction::Any).take(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; @@ -63,6 +63,7 @@ fn shortest_sequence_len(barcode: &[u8]) -> u64 { } else if Instruction::Colour(*c) == instructions[i % len] { continue; } else { + //clash so move on to the next length continue 'outer; } } @@ -72,15 +73,20 @@ fn shortest_sequence_len(barcode: &[u8]) -> u64 { unreachable!("no solution found") } -#[aoc(day4, part1)] -fn part1(input: &Vec>) -> u64 { +#[aoc(day4, part1, BFS)] +fn part1(input: &[Vec]) -> u64 { input .iter() .map(|barcode| shortest_sequence(barcode).len()) .sum::() as u64 } -#[aoc(day4, part2)] +#[aoc(day4, part1, Builder)] +fn part1_builder(input: &[Vec]) -> 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(); @@ -112,8 +118,12 @@ o??v???y #[rstest] #[case(EXAMPLE, 16)] - fn part1_example(#[case] input: &str, #[case] expected: u64) { - assert_eq!(part1(&parse(input)), expected); + fn part1_example( + #[case] input: &str, + #[case] expected: u64, + #[values(part1, part1_builder)] version: fn(&[Vec]) -> u64, + ) { + assert_eq!(version(&parse(input)), expected); } #[rstest]