perf improvements and solution for year2022 day01
This commit is contained in:
25
src/year2022/day01.rs
Normal file
25
src/year2022/day01.rs
Normal file
@ -0,0 +1,25 @@
|
||||
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!("4", part2("R8, R4, R4, R8").to_string())
|
||||
}
|
Reference in New Issue
Block a user