2026/day4: cleanup
This commit is contained in:
1 file changed
+20
-10
+20
-10
@@ -1,7 +1,6 @@
|
|||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
use cached::proc_macro::cached;
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
#[aoc_generator(day4, part1)]
|
#[aoc_generator(day4, part1)]
|
||||||
@@ -51,10 +50,11 @@ fn shortest_sequence_len(barcode: &[u8]) -> u64 {
|
|||||||
enum Instruction {
|
enum Instruction {
|
||||||
Any,
|
Any,
|
||||||
Colour(u8),
|
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() {
|
'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() {
|
for (i, c) in barcode.iter().enumerate() {
|
||||||
if *c == b'?' {
|
if *c == b'?' {
|
||||||
continue;
|
continue;
|
||||||
@@ -63,6 +63,7 @@ fn shortest_sequence_len(barcode: &[u8]) -> u64 {
|
|||||||
} else if Instruction::Colour(*c) == instructions[i % len] {
|
} else if Instruction::Colour(*c) == instructions[i % len] {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
|
//clash so move on to the next length
|
||||||
continue 'outer;
|
continue 'outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,15 +73,20 @@ fn shortest_sequence_len(barcode: &[u8]) -> u64 {
|
|||||||
unreachable!("no solution found")
|
unreachable!("no solution found")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day4, part1)]
|
#[aoc(day4, part1, BFS)]
|
||||||
fn part1(input: &Vec<Vec<u8>>) -> u64 {
|
fn part1(input: &[Vec<u8>]) -> u64 {
|
||||||
input
|
input
|
||||||
.iter()
|
.iter()
|
||||||
.map(|barcode| shortest_sequence(barcode).len())
|
.map(|barcode| shortest_sequence(barcode).len())
|
||||||
.sum::<usize>() as u64
|
.sum::<usize>() as u64
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day4, part2)]
|
#[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 {
|
fn part2(input: &str) -> u64 {
|
||||||
let barcode = input.as_bytes().iter().filter(|c| **c != b'\n').copied().collect_vec();
|
let barcode = input.as_bytes().iter().filter(|c| **c != b'\n').copied().collect_vec();
|
||||||
|
|
||||||
@@ -112,8 +118,12 @@ o??v???y
|
|||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[case(EXAMPLE, 16)]
|
#[case(EXAMPLE, 16)]
|
||||||
fn part1_example(#[case] input: &str, #[case] expected: u64) {
|
fn part1_example(
|
||||||
assert_eq!(part1(&parse(input)), expected);
|
#[case] input: &str,
|
||||||
|
#[case] expected: u64,
|
||||||
|
#[values(part1, part1_builder)] version: fn(&[Vec<u8>]) -> u64,
|
||||||
|
) {
|
||||||
|
assert_eq!(version(&parse(input)), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
|
|||||||
Reference in new issue
Block a user