2026/day4: part 2
This commit is contained in:
3 files changed
+59
-9
No files matched your search
Binary file not shown.
|
Before Width: | Height: | Size: 6.0 KiB After Width: | Height: | Size: 6.9 KiB |
+57
-7
@@ -1,8 +1,10 @@
|
|||||||
use std::collections::VecDeque;
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
use cached::proc_macro::cached;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
#[aoc_generator(day4)]
|
#[aoc_generator(day4, part1)]
|
||||||
fn parse(input: &str) -> Vec<Vec<u8>> {
|
fn parse(input: &str) -> Vec<Vec<u8>> {
|
||||||
input.lines().map(|l| l.as_bytes().to_owned()).collect()
|
input.lines().map(|l| l.as_bytes().to_owned()).collect()
|
||||||
}
|
}
|
||||||
@@ -35,7 +37,7 @@ fn shortest_sequence(barcode: &[u8]) -> Vec<u8> {
|
|||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
q.extend(possible_insts(barcode[next.len()]).into_iter().map(|c| {
|
q.extend(possible_insts(barcode[next.len()]).into_iter().map(|c| {
|
||||||
let mut new = next.clone();
|
let mut new: Vec<u8> = next.clone();
|
||||||
new.push(c);
|
new.push(c);
|
||||||
(cost + 1, new)
|
(cost + 1, new)
|
||||||
}))
|
}))
|
||||||
@@ -44,6 +46,32 @@ fn shortest_sequence(barcode: &[u8]) -> Vec<u8> {
|
|||||||
unreachable!("no solution found")
|
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, continue building until a fixed instruction clashes with a fixed barcode
|
||||||
|
'outer: for len in 1..=barcode.len() {
|
||||||
|
let mut instructions = Vec::from_iter(std::iter::repeat(Instruction::Any).take(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 {
|
||||||
|
continue 'outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instructions.len() as u64;
|
||||||
|
}
|
||||||
|
|
||||||
|
unreachable!("no solution found")
|
||||||
|
}
|
||||||
|
|
||||||
#[aoc(day4, part1)]
|
#[aoc(day4, part1)]
|
||||||
fn part1(input: &Vec<Vec<u8>>) -> u64 {
|
fn part1(input: &Vec<Vec<u8>>) -> u64 {
|
||||||
input
|
input
|
||||||
@@ -53,8 +81,10 @@ fn part1(input: &Vec<Vec<u8>>) -> u64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day4, part2)]
|
#[aoc(day4, part2)]
|
||||||
fn part2(input: &Vec<Vec<u8>>) -> u64 {
|
fn part2(input: &str) -> u64 {
|
||||||
todo!()
|
let barcode = input.as_bytes().iter().filter(|c| **c != b'\n').copied().collect_vec();
|
||||||
|
|
||||||
|
shortest_sequence_len(&barcode)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -72,6 +102,10 @@ o??v???y
|
|||||||
#[case(b"rgbrgbrgbr", b"rgb")]
|
#[case(b"rgbrgbrgbr", b"rgb")]
|
||||||
#[case(b"?rv", b"vr")]
|
#[case(b"?rv", b"vr")]
|
||||||
#[case(b"o??v???y", b"ovyvo")]
|
#[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]) {
|
fn inst_check(#[case] barcode: &[u8], #[case] instructions: &[u8]) {
|
||||||
assert!(test_instructions(barcode, instructions))
|
assert!(test_instructions(barcode, instructions))
|
||||||
}
|
}
|
||||||
@@ -83,8 +117,24 @@ o??v???y
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[case(EXAMPLE, 0)]
|
#[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) {
|
fn part2_example(#[case] input: &str, #[case] expected: u64) {
|
||||||
assert_eq!(part2(&parse(input)), expected);
|
assert_eq!(part2(input), expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
<!-- AOC TILES BEGIN -->
|
<!-- AOC TILES BEGIN -->
|
||||||
<h1 align="center">
|
<h1 align="center">
|
||||||
Advent of Code - 147/558 ⭐
|
Advent of Code - 148/558 ⭐
|
||||||
</h1>
|
</h1>
|
||||||
<h1 align="center">
|
<h1 align="center">
|
||||||
2026 - 7 ⭐ - Rust
|
2026 - 8 ⭐ - Rust
|
||||||
</h1>
|
</h1>
|
||||||
<a href="2026/src/day1.rs">
|
<a href="2026/src/day1.rs">
|
||||||
<img src=".aoc_tiles/tiles/2026/01.png" width="200">
|
<img src=".aoc_tiles/tiles/2026/01.png" width="200">
|
||||||
|
|||||||
Reference in new issue
Block a user