From ff5656b694c25297951b2bb3980aeb812effc0f4 Mon Sep 17 00:00:00 2001 From: Samuel Ortion Date: Sun, 10 Dec 2023 20:18:20 +0100 Subject: [PATCH] day5: Parse maps --- days/05/rust/part1/src/main.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/days/05/rust/part1/src/main.rs b/days/05/rust/part1/src/main.rs index ca6e4a5..30eaaa4 100644 --- a/days/05/rust/part1/src/main.rs +++ b/days/05/rust/part1/src/main.rs @@ -1,7 +1,25 @@ use std::fs; -fn parse_steps(content: &str) -> Vec>> { +fn parse_steps(lines: std::str::Lines) -> Vec>> { let mut steps: Vec>> = Vec::new(); + let mut step: Vec> = Vec::new(); + for line in lines { + if line.contains("-to-") { + step = Vec::new(); + steps.push(step); + } else if line != "" { + let numbers: Vec<&str> = line.split(" ").collect(); + let numbers: Vec = numbers + .iter() + .map(|string| { + let number: u32 = string.parse().unwrap(); + number + }) + .collect(); + let n = steps.len(); + steps[n - 1].push(numbers); + } + } steps } @@ -19,6 +37,10 @@ fn main() { .expect("Should have a seed") .split(" ") .collect(); + let steps = parse_steps(lines); + for step in steps { + println!("{:?}", step); + } let seeds: Vec = seeds .iter() .map(|string| {