diff --git a/2024/rust/src/day5.rs b/2024/rust/src/day5.rs new file mode 100644 index 0000000..0d825c1 --- /dev/null +++ b/2024/rust/src/day5.rs @@ -0,0 +1,50 @@ +mod prelude; +pub use crate::prelude::*; + +fn main() { + let input = day_input(5); + let update: ManualUpdate = input.parse().unwrap(); + println!("{update:?}"); + show_answers(0, 0); +} + +#[derive(Debug)] +struct ManualUpdate { + rules: HashMap>, + updates: Vec>, +} + +impl FromStr for ManualUpdate { + type Err = ParseIntError; + + fn from_str(s: &str) -> Result { + let (rules_string, updates_string) = s.split_once("\n\n").unwrap(); + let mut rules: HashMap> = HashMap::new(); + let mut updates = vec![]; + for rule in rules_string.lines() { + let (left, right) = rule.split_once("|").unwrap(); + let (left, right): (i64, i64) = (left.parse()?, right.parse()?); + + rules.entry(left).or_default().push(right); + } + for update in updates_string.lines() { + println!("{update}"); + updates.push( + update + .split(",") + .map(|page| page.parse()) + .collect::, Self::Err>>()?, + ); + } + + Ok(Self { rules, updates }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test() {} +}