use aoc_runner_derive::{aoc, aoc_generator}; use itertools::Itertools; use std::{ collections::{HashMap, HashSet, VecDeque}, 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 { 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 { 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, 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, }; if displayed > Self::MAX_N { Self { displayed: self.displayed, } } else { Self { displayed } } } } #[aoc_generator(day5)] fn parse(input: &str) -> Vec