2026/day5: part 1

This commit is contained in:
ktims committed 2026-12-04 23:02:58 -07:00
1 parent b63e5b478b
commit 9083529b28
4 files changed
+133 -2

No files matched your search

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

+127
View File
@@ -0,0 +1,127 @@
use aoc_runner_derive::{aoc, aoc_generator};
use std::{collections::HashMap, fmt::Display, str::FromStr};
#[derive(Clone, Copy, Debug, PartialEq)]
enum Button {
Append(u8),
Mult(u8),
Div(u8),
Replace((u8, u8)),
Oct,
Bksp,
}
impl FromStr for Button {
type Err = std::str::Utf8Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = &s.as_bytes()[1..s.len() - 1];
match s[0] {
b'*' => Ok(Self::Mult(s[1] - b'0')),
b'/' => Ok(Self::Div(s[1] - b'0')),
b'o' => Ok(Self::Oct),
b'b' => Ok(Self::Bksp),
b if s.len() > 1 => Ok(Self::Replace((b - b'0', s[3] - b'0'))),
b => Ok(Self::Append(b - b'0')),
}
}
}
impl Display for Button {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Append(i) => write!(f, "[{i}]"),
Self::Mult(i) => write!(f, "[*{i}]"),
Self::Div(i) => write!(f, "[/{i}]"),
Self::Replace((a, b)) => write!(f, "[{a}->{b}]"),
Self::Oct => write!(f, "[oct]"),
Self::Bksp => write!(f, "[bksp]"),
}
}
}
#[derive(Clone, Debug)]
struct Microwave {
displayed: u64,
}
impl Microwave {
fn press(&self, button: Button) -> Microwave {
let displayed = match button {
Button::Append(i) => self.displayed * 10 + i as u64,
Button::Mult(i) => self.displayed * i as u64,
Button::Div(i) => self.displayed / i as u64,
Button::Replace((from, to)) => self
.displayed
.to_string()
.replace(&from.to_string(), &to.to_string())
.parse()
.unwrap(),
Button::Oct => format!("{:o}", self.displayed).parse().unwrap(),
Button::Bksp => self.displayed / 10,
};
Self { displayed }
}
}
#[aoc_generator(day5)]
fn parse(input: &str) -> Vec<Button> {
input.split_ascii_whitespace().map(|b| b.parse().unwrap()).collect()
}
fn mark_reachable_from(
reachable: &mut HashMap<u64, Vec<Button>>,
state: &Microwave,
max: usize,
buttons: &[Button],
steps: Vec<Button>,
) {
if let Some(already_steps) = reachable.get(&state.displayed) {
if steps.len() >= already_steps.len() {
return;
}
}
reachable.insert(state.displayed, steps.clone());
if steps.len() == max {
return;
}
for but in buttons {
let mut steps = steps.clone();
steps.push(*but);
mark_reachable_from(reachable, &state.press(*but), max, buttons, steps);
}
}
#[aoc(day5, part1)]
fn part1(input: &[Button]) -> u64 {
let mut reachable = HashMap::new();
// DFS, mark all reached numbers, if we see something again we have already traversed it and can skip the rest fo the tree
mark_reachable_from(&mut reachable, &Microwave { displayed: 0 }, 3, input, Vec::new());
reachable.keys().sum()
}
#[aoc(day5, part2)]
fn part2(input: &[Button]) -> u64 {
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
const EXAMPLE: &str = "[9] [*9] [/2] [1->9] [oct] [bksp]";
#[rstest]
#[case(EXAMPLE, 4268)]
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
View File
@@ -2,6 +2,7 @@ mod day1;
mod day2;
mod day3;
mod day4;
mod day5;
use aoc_runner_derive::aoc_lib;
+5 -2
View File
@@ -1,9 +1,9 @@
<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 148/560 ⭐
Advent of Code - 149/560 ⭐
</h1>
<h1 align="center">
2026 - 8 ⭐ - Rust
2026 - 9 ⭐ - Rust
</h1>
<a href="2026/src/day1.rs">
<img src=".aoc_tiles/tiles/2026/01.png" width="200">
@@ -17,6 +17,9 @@
<a href="2026/src/day4.rs">
<img src=".aoc_tiles/tiles/2026/04.png" width="200">
</a>
<a href="2026/src/day5.rs">
<img src=".aoc_tiles/tiles/2026/05.png" width="200">
</a>
<h1 align="center">
2025 - 22 ⭐ - Rust
</h1>