From 9d0fc7d649141d173511ec84de7944b4c7079451 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 10 Oct 2023 16:45:14 +0200 Subject: [PATCH] Algorithm to gather each kcal from elves --- day1/src/main.rs | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/day1/src/main.rs b/day1/src/main.rs index 111ac74..7fe9bd0 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -5,26 +5,48 @@ mod elves; fn main() { - let elf_vec = vec![elves::Elf::new(vec![1000,2000,3000]),elves::Elf::new(vec![4000]), elves::Elf::new(vec![5000,6000]), elves::Elf::new(vec![7000,8000,9000]), elves::Elf::new(vec![10000])]; - for elf in &elf_vec - { - println!("Elf :"); - elf.print_inventory() - } - + // Variables + let mut vec_elf : Vec = Vec::new(); let mut file; + // Checking if the file is found. match File::open("elves") { Ok(actualfile) => { file = actualfile; } Err(fileerr) => { - panic!("Could not open file 'elves' : {}", fileerr) + panic!("Could not open file 'elves': {}", fileerr) } }; // There will be content if the file is read so we unwrap. let mut file_content = String::new(); file.read_to_string(&mut file_content).unwrap(); - println!("{}", file_content); + + // Filtering the given string into multiple vectors + let mut s = String::new(); + let mut vec_kcal: Vec = Vec::new(); + for ch in file_content.chars() { + if ch == '\n' { + match s.parse() { + Ok(parsed_num) => { + vec_kcal.push(parsed_num); + } + Err(_) => { + vec_elf.push(elves::Elf::new(vec_kcal)); + vec_kcal = Vec::new(); + } + } + s.clear(); + } else { + if ch != '\r' { + s.push(ch) + } + } + } + + for elf in &vec_elf { + println!("Elf: "); + elf.print_inventory(); + } }