finish day 1, part 2

This commit is contained in:
Max Bradbury 2022-12-01 20:01:30 +00:00
parent 69ff24d2cd
commit 907c7307ac
2 changed files with 19 additions and 6 deletions

View File

@ -1,8 +1,4 @@
use std::fs; pub fn part_1(input: &String) -> String {
pub fn day_1() -> String {
let input = fs::read_to_string("input/day_1.txt").unwrap();
let elves: Vec<&str> = input.split("\n\n").collect(); let elves: Vec<&str> = input.split("\n\n").collect();
let mut highest_calorie_count: u64 = 0; let mut highest_calorie_count: u64 = 0;
@ -21,3 +17,18 @@ pub fn day_1() -> String {
format!("{highest_calorie_count}") format!("{highest_calorie_count}")
} }
pub fn part_2(input: &String) -> u64 {
let mut elf_calorie_counts: Vec<u64> = 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()
}

View File

@ -1,5 +1,7 @@
mod day_1; mod day_1;
fn main() { 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));
} }