diff --git a/.aoc_tiles/tiles/2026/05.png b/.aoc_tiles/tiles/2026/05.png new file mode 100644 index 0000000..42db858 Binary files /dev/null and b/.aoc_tiles/tiles/2026/05.png differ diff --git a/2026/src/day5.rs b/2026/src/day5.rs new file mode 100644 index 0000000..237ad68 --- /dev/null +++ b/2026/src/day5.rs @@ -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 { + 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