From 907c7307acdacb5d6edb42b04dc210761afbb81a Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Thu, 1 Dec 2022 20:01:30 +0000 Subject: [PATCH] finish day 1, part 2 --- src/day_1.rs | 21 ++++++++++++++++----- src/main.rs | 4 +++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/day_1.rs b/src/day_1.rs index b0de651..b336339 100644 --- a/src/day_1.rs +++ b/src/day_1.rs @@ -1,8 +1,4 @@ -use std::fs; - -pub fn day_1() -> String { - let input = fs::read_to_string("input/day_1.txt").unwrap(); - +pub fn part_1(input: &String) -> String { let elves: Vec<&str> = input.split("\n\n").collect(); let mut highest_calorie_count: u64 = 0; @@ -21,3 +17,18 @@ pub fn day_1() -> String { format!("{highest_calorie_count}") } + +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(); + + elf_calorie_counts.sort_unstable(); + + let start = elf_calorie_counts.len() - 3; + let top_3 = elf_calorie_counts.drain(start..); + + println!("Top 3 elves: {:?}", top_3); + + top_3.sum() +} diff --git a/src/main.rs b/src/main.rs index abf3ef9..e294e4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ mod day_1; fn main() { - println!("{}", day_1::day_1()); + let day_1_input = std::fs::read_to_string("input/day_1.txt").unwrap(); + println!("Day 1, part 1: {}", day_1::part_1(&day_1_input)); + println!("Day 1, part 2: {:?}", day_1::part_2(&day_1_input)); }