diff --git a/aoc2022/src/day01.rs b/aoc2022/src/day01.rs index 57e623f..230ce57 100644 --- a/aoc2022/src/day01.rs +++ b/aoc2022/src/day01.rs @@ -1,3 +1,4 @@ +use std::collections::BinaryHeap; use std::fmt::Write; use anyhow::{Context, Result}; @@ -8,6 +9,7 @@ pub fn run() -> Result { let mut res = String::with_capacity(128); writeln!(res, "part 1: {}", part1(INPUT)?)?; + writeln!(res, "part 2: {}", part2(INPUT)?)?; Ok(res) } @@ -25,6 +27,16 @@ fn part1(input: &str) -> Result { .context("inventory list was empty") } +fn part2(input: &str) -> Result { + let inventories = input + .split("\n\n") + .map(|line| line.parse::().map(|inv| inv.total_calories())) + .collect::>>()?; + + Ok(inventories.iter().take(3).sum()) +} + +#[derive(Debug)] struct Inventory(Vec); impl std::str::FromStr for Inventory { @@ -60,4 +72,14 @@ mod tests { fn part1_real() { assert_eq!(part1(INPUT).unwrap(), 68923); } + + #[test] + fn part2_provided() { + assert_eq!(part2(PROVIDED).unwrap(), 45000); + } + + #[test] + fn part2_real() { + assert_eq!(part2(INPUT).unwrap(), 200044); + } }