2026/day4: slight simplification / optimization
This commit is contained in:
2 files changed
+6
-10
No files matched your search
+5
-9
@@ -46,21 +46,17 @@ fn shortest_sequence(barcode: &[u8]) -> Vec<u8> {
|
||||
}
|
||||
|
||||
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));
|
||||
let mut instructions = barcode[..len].to_vec();
|
||||
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] {
|
||||
} else if instructions[i % len] == b'?' {
|
||||
instructions[i % len] = *c
|
||||
} else if *c == instructions[i % len] {
|
||||
continue;
|
||||
} else {
|
||||
//clash so move on to the next length
|
||||
|
||||
Reference in new issue
Block a user