2026/day4: slight simplification / optimization

This commit is contained in:
ktims committed 2026-12-04 17:42:46 -07:00
1 parent 464da9d787
commit b63e5b478b
2 files changed
+6 -10

No files matched your search

+5 -9
View File
@@ -46,21 +46,17 @@ fn shortest_sequence(barcode: &[u8]) -> Vec<u8> {
} }
fn shortest_sequence_len(barcode: &[u8]) -> u64 { 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 // 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 // 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_n(Instruction::Any, len)); let mut instructions = barcode[..len].to_vec();
for (i, c) in barcode.iter().enumerate() { for (i, c) in barcode.iter().enumerate() {
if *c == b'?' { if *c == b'?' {
continue; continue;
} else if instructions[i % len] == Instruction::Any { } else if instructions[i % len] == b'?' {
instructions[i % len] = Instruction::Colour(*c) instructions[i % len] = *c
} else if Instruction::Colour(*c) == instructions[i % len] { } else if *c == instructions[i % len] {
continue; continue;
} else { } else {
//clash so move on to the next length //clash so move on to the next length
+1 -1
View File
@@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN --> <!-- AOC TILES BEGIN -->
<h1 align="center"> <h1 align="center">
Advent of Code - 148/558 ⭐ Advent of Code - 148/560 ⭐
</h1> </h1>
<h1 align="center"> <h1 align="center">
2026 - 8 ⭐ - Rust 2026 - 8 ⭐ - Rust