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

23 lines
575 B
Rust
Raw Normal View History

2022-12-03 08:53:34 +00:00
fn elf_calorie_counts(input: &str) -> Vec<u64> {
2022-12-02 00:24:50 +00:00
input.split("\n\n").map(|elf| {
elf.split_whitespace().map(|meal| meal.parse().unwrap_or(0)).sum()
}).collect()
}
2022-12-01 19:03:15 +00:00
2022-12-03 08:53:34 +00:00
pub fn part_1(input: &str) -> u64 {
2022-12-02 00:24:50 +00:00
let mut elf_calorie_counts= elf_calorie_counts(input);
2022-12-01 19:03:15 +00:00
2022-12-02 00:24:50 +00:00
elf_calorie_counts.sort_unstable();
2022-12-01 19:03:15 +00:00
2022-12-02 00:24:50 +00:00
elf_calorie_counts.pop().unwrap()
2022-12-01 19:03:15 +00:00
}
2022-12-01 20:01:30 +00:00
2022-12-03 08:53:34 +00:00
pub fn part_2(input: &str) -> u64 {
2022-12-02 00:24:50 +00:00
let mut elf_calorie_counts= elf_calorie_counts(input);
2022-12-01 20:01:30 +00:00
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
}