blob: 078eb25f037b87ab9beb85df108e85f09fc6c7a2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// AoC 2022 - day 1.
fn sum_elf(elf: &str) -> usize {
elf.lines()
.map(|s| s.parse::<usize>().expect("invalid input"))
.sum()
}
fn group_by_elf(input: &str) -> Vec<usize> {
input.rsplit("\n\n").map(sum_elf).collect()
}
fn top_elf(input: &str) -> usize {
group_by_elf(&input).into_iter().max().unwrap()
}
fn top_n_elves(n: usize, input: &str) -> usize {
let mut by_elf = group_by_elf(input);
by_elf.sort_by(|a, b| b.cmp(a)); // high->low
(by_elf[..n]).iter().sum()
}
fn main() {
let input = std::fs::read_to_string("input").expect("input should be in file named 'input'");
println!("top elf: {}", top_elf(&input));
println!("top 3 elves: {}", top_n_elves(3, &input));
}
|