Skip to main content

advent_of_code/year2022/
day01.rs

1use crate::common::highest_values::HighestValues;
2use crate::input::Input;
3
4pub fn solve(input: &Input) -> Result<u64, String> {
5    if input.is_part_one() {
6        solve_part::<1>(input)
7    } else {
8        solve_part::<3>(input)
9    }
10}
11
12pub fn solve_part<const NUM: usize>(input: &Input) -> Result<u64, String> {
13    Ok(input
14        .text
15        .split("\n\n")
16        .map(|elf| {
17            elf.lines()
18                .map(|line| u64::from(line.parse::<u32>().unwrap_or_default()))
19                .sum()
20        })
21        .collect::<HighestValues<NUM>>()
22        .sum())
23}
24
25#[test]
26pub fn tests() {
27    let test_input = "1000\n\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000";
28    test_part_one_no_allocations!(test_input => 24_000);
29    test_part_two_no_allocations!(test_input => 45_000);
30
31    let test_input = "4294967296";
32    test_part_one_no_allocations!(test_input => 0);
33    test_part_two_no_allocations!(test_input => 0);
34
35    let test_input = "4294967295\n\n1\n\n1";
36    test_part_one_no_allocations!(test_input => 4_294_967_295);
37    test_part_two_no_allocations!(test_input => 4_294_967_297);
38
39    let real_input = include_str!("day01_input.txt");
40    test_part_one_no_allocations!(real_input => 71_300);
41    test_part_two_no_allocations!(real_input => 209_691);
42}