2026/day4: part1
This commit is contained in:
5 files changed
+97
-3
No files matched your search
Binary file not shown.
|
Before Width: | Height: | Size: 8.3 KiB After Width: | Height: | Size: 9.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 6.0 KiB |
@@ -0,0 +1,90 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
|
||||
#[aoc_generator(day4)]
|
||||
fn parse(input: &str) -> Vec<Vec<u8>> {
|
||||
input.lines().map(|l| l.as_bytes().to_owned()).collect()
|
||||
}
|
||||
|
||||
const OPTIONS: &[u8] = b"roygbv";
|
||||
|
||||
fn test_instructions(barcode: &[u8], instructions: &[u8]) -> bool {
|
||||
instructions
|
||||
.iter()
|
||||
.cycle()
|
||||
.zip(barcode.iter())
|
||||
.all(|(inst, code)| *code == b'?' || code == inst)
|
||||
}
|
||||
|
||||
fn possible_insts(code: u8) -> Vec<u8> {
|
||||
if code == b'?' { OPTIONS.to_owned() } else { vec![code] }
|
||||
}
|
||||
|
||||
fn shortest_sequence(barcode: &[u8]) -> Vec<u8> {
|
||||
// use bfs to find the lowest cost matching instruction set
|
||||
//
|
||||
// nodes - instruction sequences, edges cost 1, return on finding a sequence
|
||||
// where test_instructions passes
|
||||
|
||||
let mut q = VecDeque::new();
|
||||
q.extend(possible_insts(barcode[0]).into_iter().map(|c| (1, vec![c])));
|
||||
|
||||
while let Some((cost, next)) = q.pop_front() {
|
||||
if test_instructions(barcode, &next) {
|
||||
return next;
|
||||
}
|
||||
q.extend(possible_insts(barcode[next.len()]).into_iter().map(|c| {
|
||||
let mut new = next.clone();
|
||||
new.push(c);
|
||||
(cost + 1, new)
|
||||
}))
|
||||
}
|
||||
|
||||
unreachable!("no solution found")
|
||||
}
|
||||
|
||||
#[aoc(day4, part1)]
|
||||
fn part1(input: &Vec<Vec<u8>>) -> u64 {
|
||||
input
|
||||
.iter()
|
||||
.map(|barcode| shortest_sequence(barcode).len())
|
||||
.sum::<usize>() as u64
|
||||
}
|
||||
|
||||
#[aoc(day4, part2)]
|
||||
fn part2(input: &Vec<Vec<u8>>) -> u64 {
|
||||
todo!()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rstest::rstest;
|
||||
|
||||
const EXAMPLE: &str = "rgbrgbrgbr
|
||||
?rv
|
||||
o??v???y
|
||||
??bg?v??
|
||||
??b?????
|
||||
???";
|
||||
#[rstest]
|
||||
#[case(b"rgbrgbrgbr", b"rgb")]
|
||||
#[case(b"?rv", b"vr")]
|
||||
#[case(b"o??v???y", b"ovyvo")]
|
||||
fn inst_check(#[case] barcode: &[u8], #[case] instructions: &[u8]) {
|
||||
assert!(test_instructions(barcode, instructions))
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(EXAMPLE, 16)]
|
||||
fn part1_example(#[case] input: &str, #[case] expected: u64) {
|
||||
assert_eq!(part1(&parse(input)), expected);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(EXAMPLE, 0)]
|
||||
fn part2_example(#[case] input: &str, #[case] expected: u64) {
|
||||
assert_eq!(part2(&parse(input)), expected);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod day1;
|
||||
mod day2;
|
||||
mod day3;
|
||||
mod day4;
|
||||
|
||||
use aoc_runner_derive::aoc_lib;
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
<!-- AOC TILES BEGIN -->
|
||||
<h1 align="center">
|
||||
Advent of Code - 145/558 ⭐
|
||||
Advent of Code - 147/558 ⭐
|
||||
</h1>
|
||||
<h1 align="center">
|
||||
2026 - 6 ⭐ - Rust
|
||||
2026 - 7 ⭐ - Rust
|
||||
</h1>
|
||||
<a href="2026/src/day1.rs">
|
||||
<img src=".aoc_tiles/tiles/2026/01.png" width="200">
|
||||
@@ -14,6 +14,9 @@
|
||||
<a href="2026/src/day3.rs">
|
||||
<img src=".aoc_tiles/tiles/2026/03.png" width="200">
|
||||
</a>
|
||||
<a href="2026/src/day4.rs">
|
||||
<img src=".aoc_tiles/tiles/2026/04.png" width="200">
|
||||
</a>
|
||||
<h1 align="center">
|
||||
2025 - 22 ⭐ - Rust
|
||||
</h1>
|
||||
@@ -210,7 +213,7 @@
|
||||
<img src=".aoc_tiles/tiles/2023/25.png" width="200">
|
||||
</a>
|
||||
<h1 align="center">
|
||||
2022 - 15 ⭐ - Rust
|
||||
2022 - 16 ⭐ - Rust
|
||||
</h1>
|
||||
<a href="2022/src/day1.rs">
|
||||
<img src=".aoc_tiles/tiles/2022/01.png" width="200">
|
||||
|
||||
Reference in new issue
Block a user