Files
advent-of-code-2022/src/day_1.rs

31 lines
817 B
Rust
Raw Normal View History

2022-12-01 21:41:54 +00:00
pub fn part_1(input: &String) -> u64 {
2022-12-01 19:03:15 +00:00
let elves: Vec<&str> = input.split("\n\n").collect();
let mut highest_calorie_count: u64 = 0;
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;
}
}
2022-12-01 21:41:54 +00:00
highest_calorie_count
2022-12-01 19:03:15 +00:00
}
2022-12-01 20:01:30 +00:00
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();
2022-12-02 00:20:08 +00:00
elf_calorie_counts.reverse();
2022-12-01 20:01:30 +00:00
2022-12-02 00:20:08 +00:00
elf_calorie_counts.iter().take(3).sum()
2022-12-01 20:01:30 +00:00
}