From 2c055a1094076b81387cf5c6a1d183a46abeda03 Mon Sep 17 00:00:00 2001 From: Bizcochito Date: Thu, 1 Dec 2022 11:09:43 +0100 Subject: [PATCH] wow this thing have more than one part --- day1/src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/day1/src/main.rs b/day1/src/main.rs index 4d33db3..24f266c 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -6,10 +6,15 @@ fn main() { println!("Hi this is the fist day of AOC2022, first we will read the file {}", FILE_PATH); let contents = fs::read_to_string(FILE_PATH) .expect("Should have been able to read the file"); - println!("The elf with the biggest calories is: {}", get_max_elf(&contents)); + + println!("And we get the elfs from the file!"); + let mut elfs = get_elfs(&contents); + println!("The elf with the biggest calories is carring: {}", get_max_elf(&elfs)); + println!("The elf with the 3 biggest calories are carring: {}", get_max_3_elf(&mut elfs)); + } -fn get_max_elf(input: &str) -> u32{ +fn get_elfs(input: &str) -> Vec { let binding = input.to_owned(); let input: Vec<&str> = binding.lines().collect(); let mut elfs = Vec::new(); @@ -19,9 +24,22 @@ fn get_max_elf(input: &str) -> u32{ if line == "" {ptr+=1; elfs.push(0); continue;} elfs[ptr] += u32::from_str(line).expect("all content must be numbers"); } + elfs +} + +fn get_max_elf(elfs: &Vec) -> u32{ *elfs.iter().max().expect("there must be a maximum lol") } +fn get_max_3_elf(elfs: &mut Vec) -> u32{ + let mut out: u32 = 0; + elfs.sort(); + for _ in 0..3 { + out += elfs.pop().expect("There must be at least 3 elfs"); + } + out +} + #[cfg(test)] mod test { use super::*; @@ -41,7 +59,26 @@ mod test { 9000 10000"#; - - assert_eq!(get_max_elf(input), 24000); + let tst = get_elfs(input); + assert_eq!(get_max_elf(&tst), 24000); + } + #[test] + fn it_works2() { + let input = r#"1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000"#; + let mut tst = get_elfs(input); + assert_eq!(get_max_3_elf(&mut tst), 45000); } } \ No newline at end of file