Parsing
This commit is contained in:
parent
80315133c4
commit
dc5e3b450f
50
2024/rust/src/day5.rs
Normal file
50
2024/rust/src/day5.rs
Normal file
|
@ -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<i64, Vec<i64>>,
|
||||
updates: Vec<Vec<i64>>,
|
||||
}
|
||||
|
||||
impl FromStr for ManualUpdate {
|
||||
type Err = ParseIntError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let (rules_string, updates_string) = s.split_once("\n\n").unwrap();
|
||||
let mut rules: HashMap<i64, Vec<i64>> = 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::<Result<Vec<i64>, Self::Err>>()?,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(Self { rules, updates })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test() {}
|
||||
}
|
Loading…
Reference in a new issue