From dc5e3b450f66b7fe552b4bafcb81db3271b6325a Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Thu, 5 Dec 2024 10:04:24 -0600 Subject: [PATCH] Parsing --- 2024/rust/src/day5.rs | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2024/rust/src/day5.rs 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() {} +}