aoc/src/year2022/day01.rs

32 lines
774 B
Rust

use crate::util::parse::ParseExt;
fn calories(input: &str) -> impl Iterator<Item = u32> + '_ {
input.split("\n\n").map(|set| set.u32s().sum())
}
pub fn part1(input: &str) -> impl std::fmt::Display {
calories(input).max().expect("must have one max")
}
pub fn part2(input: &str) -> impl std::fmt::Display {
let mut items: Vec<_> = calories(input).collect();
items.sort_unstable();
items.iter().rev().take(3).sum::<u32>()
}
#[test]
fn test_part1() {
assert_eq!(
"24000",
part1("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()
)
}
#[test]
fn test_part2() {
assert_eq!(
"45000",
part2("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()
)
}