diff --git a/2022/01/2/rust/Cargo.toml b/2022/01/2/rust/Cargo.toml new file mode 100644 index 0000000..899fce9 --- /dev/null +++ b/2022/01/2/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc-2022-01-1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2022/01/2/rust/src/main.rs b/2022/01/2/rust/src/main.rs new file mode 100644 index 0000000..0014fe7 --- /dev/null +++ b/2022/01/2/rust/src/main.rs @@ -0,0 +1,142 @@ +use std::{ + collections::BinaryHeap, + fmt::Display, + io::{self, Read}, +}; + +#[derive(Debug, Clone)] +struct AocError { + pub msg: String, +} + +impl From for AocError { + fn from(value: io::Error) -> Self { + Self { + msg: value.to_string(), + } + } +} + +impl Display for AocError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.msg) + } +} + +/// Inv describes the inversion of something, e.g. the negation operation on a boolean: +/// +/// ``` +/// impl Inv for bool { +/// fn invert(&self) -> Self { !self } +/// } +/// ``` +pub trait Inv { + /// Invert the given element. + /// + /// ``` + /// let x: i32 = 12; + /// println!("{}", x.invert()); // -> -12 + /// ``` + /// + /// Ensure that `x == x.invert().invert()`. + fn invert(&self) -> Self; +} + +impl Inv for u64 { + fn invert(&self) -> Self { + u64::MAX - self + } +} + +/// NLargest is a rolling container for the (at most) `n` largest items which +/// are pushed through it. +pub struct NLargest +where + T: Ord, +{ + n: usize, + h: BinaryHeap, +} + +impl NLargest +where + T: Ord, + T: Inv, +{ + /// Create a new `NLargest` for the given `n`. + pub fn new(n: usize) -> Self { + NLargest { + n, + h: BinaryHeap::with_capacity(n), + } + } + + /// Push a new item through this `NLargest`. + /// + /// When an item is pushed through it while it already holds `n` values, it is only retained if + /// it is larger than the smallest previously retained value, supplanting it. + /// (When there are not yet `n` items retained, any pushed item is retained.) + pub fn push(&mut self, item: T) { + if self.h.len() < self.n { + self.h.push(item.invert()); + } else if item.cmp(&self.h.peek().unwrap().invert()).is_gt() { + self.h.pop(); + self.h.push(item.invert()); + } + } +} + +impl IntoIterator for NLargest +where + T: Ord, + T: Inv, +{ + type Item = T; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.h + .into_iter() + .map(|x| x.invert()) + .collect::>() + .into_iter() + } +} + +fn main() -> Result<(), AocError> { + let mut s: String = String::default(); + if std::io::stdin().read_to_string(&mut s)? == 0 { + return Err(AocError { + msg: "read too little?".to_string(), + }); + }; + + // declare our helper container which will have all values pushed through it and retain the `n` + // largest, where `n` in our case is `3` + let mut three_max_values: NLargest = NLargest::new(3); + + // push all elf payload sums through our helper container + s.split("\n\n") + .map(|block| { + block.split('\n').filter_map(|value| { + if value.is_empty() { + return None; + } + match value.parse::() { + Ok(v) => Some(v), + Err(e) => { + panic!("could not parse '{value}' ({e}); assuming that's an error."); + } + } + }) + }) + .map(|elf_payload| elf_payload.sum::()) + .for_each(|item| three_max_values.push(item)); + + // sum up the (3) maximal values we have retained + let max: u64 = three_max_values.into_iter().sum(); + + println!("{max}"); + + Ok(()) +}