From bc0ba67c162b9e2b929f8eec66752232455cd35d Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Fri, 2 Dec 2022 00:24:50 +0000 Subject: [PATCH] dedupe and improve --- src/day_1.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/day_1.rs b/src/day_1.rs index 2e61ddb..9bd8f56 100644 --- a/src/day_1.rs +++ b/src/day_1.rs @@ -1,27 +1,19 @@ +fn elf_calorie_counts(input: &String) -> Vec { + input.split("\n\n").map(|elf| { + elf.split_whitespace().map(|meal| meal.parse().unwrap_or(0)).sum() + }).collect() +} + pub fn part_1(input: &String) -> u64 { - let elves: Vec<&str> = input.split("\n\n").collect(); + let mut elf_calorie_counts= elf_calorie_counts(input); - let mut highest_calorie_count: u64 = 0; + elf_calorie_counts.sort_unstable(); - for meals in elves.iter() { - let mut elf_calorie_count= 0; - - for meal in meals.split_whitespace() { - elf_calorie_count += meal.parse().unwrap_or(0); - } - - if elf_calorie_count > highest_calorie_count { - highest_calorie_count = elf_calorie_count; - } - } - - highest_calorie_count + elf_calorie_counts.pop().unwrap() } pub fn part_2(input: &String) -> u64 { - let mut elf_calorie_counts: Vec = input.split("\n\n").map(|elf| { - elf.split_whitespace().map(|meal| meal.parse().unwrap_or(0)).sum() - }).collect(); + let mut elf_calorie_counts= elf_calorie_counts(input); elf_calorie_counts.sort_unstable(); elf_calorie_counts.reverse();