2026/day5: part2 - facepalm why did i choose dfs??
This commit is contained in:
1 file changed
+35
-8
+35
-8
@@ -1,6 +1,11 @@
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
use itertools::Itertools;
|
||||
|
||||
use std::{collections::HashMap, fmt::Display, str::FromStr};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet, VecDeque},
|
||||
fmt::Display,
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum Button {
|
||||
@@ -41,12 +46,13 @@ impl Display for Button {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Microwave {
|
||||
struct Microwave<const DIGITS: u32> {
|
||||
displayed: u64,
|
||||
}
|
||||
|
||||
impl Microwave {
|
||||
fn press(&self, button: Button) -> Microwave {
|
||||
impl<const DIGITS: u32> Microwave<DIGITS> {
|
||||
const MAX_N: u64 = 10u64.pow(DIGITS) - 1;
|
||||
fn press(&self, button: Button) -> Self {
|
||||
let displayed = match button {
|
||||
Button::Append(i) => self.displayed * 10 + i as u64,
|
||||
Button::Mult(i) => self.displayed * i as u64,
|
||||
@@ -60,8 +66,14 @@ impl Microwave {
|
||||
Button::Oct => format!("{:o}", self.displayed).parse().unwrap(),
|
||||
Button::Bksp => self.displayed / 10,
|
||||
};
|
||||
if displayed > Self::MAX_N {
|
||||
Self {
|
||||
displayed: self.displayed,
|
||||
}
|
||||
} else {
|
||||
Self { displayed }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[aoc_generator(day5)]
|
||||
@@ -71,7 +83,7 @@ fn parse(input: &str) -> Vec<Button> {
|
||||
|
||||
fn mark_reachable_from(
|
||||
reachable: &mut HashMap<u64, Vec<Button>>,
|
||||
state: &Microwave,
|
||||
state: &Microwave<19>,
|
||||
max: usize,
|
||||
buttons: &[Button],
|
||||
steps: Vec<Button>,
|
||||
@@ -97,13 +109,28 @@ 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());
|
||||
mark_reachable_from(&mut reachable, &Microwave::<19> { displayed: 0 }, 3, input, Vec::new());
|
||||
reachable.keys().sum()
|
||||
}
|
||||
|
||||
#[aoc(day5, part2)]
|
||||
fn part2(input: &[Button]) -> u64 {
|
||||
todo!()
|
||||
let mut reachable = HashSet::new();
|
||||
let mut q = VecDeque::new();
|
||||
|
||||
let state = Microwave::<4> { displayed: 0 };
|
||||
q.extend(input.iter().map(|b| (state.clone(), b)));
|
||||
|
||||
while let Some(next) = q.pop_front() {
|
||||
let new = next.0.press(*next.1);
|
||||
if reachable.contains(&new.displayed) {
|
||||
continue;
|
||||
}
|
||||
reachable.insert(new.displayed);
|
||||
q.extend(input.iter().map(|b| (new.clone(), b)));
|
||||
}
|
||||
|
||||
reachable.iter().sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -120,7 +147,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(EXAMPLE, 0)]
|
||||
#[case(EXAMPLE, 35332647)]
|
||||
fn part2_example(#[case] input: &str, #[case] expected: u64) {
|
||||
assert_eq!(part2(&parse(input)), expected);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user