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)); }