aoc/year2022/
day01.rs

1//! # Calorie Counting
2//! Sums groups of numbers separated by blank lines into an `vec` sorted in ascending order.
3//!
4//! Since we don't care what order the highest values are returned in [`select_nth_unstable`] would
5//! also work, and in theory is a little faster, however the difference was negligible when benchmarking.
6//!
7//! [`select_nth_unstable`]: slice::select_nth_unstable
8use crate::util::parse::*;
9
10/// Parse and group lines.
11pub fn parse(input: &str) -> Vec<u32> {
12    let mut elves: Vec<u32> = input.split("\n\n").map(|s| s.iter_unsigned::<u32>().sum()).collect();
13    elves.sort_unstable();
14    elves
15}
16
17/// Use a reverse iterator to find the elf with the most calories.
18pub fn part1(input: &[u32]) -> u32 {
19    input.iter().rev().take(1).sum()
20}
21
22/// Use a reverse iterator to sum the calories of the 3 highest elves.
23pub fn part2(input: &[u32]) -> u32 {
24    input.iter().rev().take(3).sum()
25}