From 5c63fdf294c336bd47b63da26c5706f3649439f5 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 1 Dec 2022 18:51:42 +0100 Subject: [PATCH] Solve Part 2 --- src/main.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index e87b19a..db5b3bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,15 +2,21 @@ const DATA: &'static str = include_str!("input.txt"); +fn parse_calories_per_elf(elf: &str) -> i32 { + let mut elf_sum = 0; + for food_item in elf.split("\n") + { + elf_sum += food_item.parse().unwrap_or(0); + } + elf_sum +} + +#[allow(dead_code)] fn part1() { let mut highest_sum = 0; for elf in DATA.split("\n\n") { - let mut elf_sum = 0; - for food_item in elf.split("\n") - { - elf_sum += food_item.parse().unwrap_or(0); - } + let elf_sum = parse_calories_per_elf(elf); if elf_sum > highest_sum { highest_sum = elf_sum; } @@ -18,7 +24,25 @@ fn part1() { println!("The elf carrying the most calories carries {} calories", highest_sum); } +fn part2() { + let mut elves: Vec = Vec::new(); + + for elf in DATA.split("\n\n") + { + let elf_sum = parse_calories_per_elf(elf); + elves.push(elf_sum); + } + + elves.sort(); + + // Get the 3 elves carrying more calories + let slice = &elves[elves.len()-3 ..]; + + println!("The 3 elves carrying the most calories carry {} calories", slice.iter().sum::()); +} + fn main() { - part1(); + //part1(); + part2(); }