Function aoc::y22d01::y22d01

source ·
pub fn y22d01(input: &str, top: u32) -> u32
Expand description

The solution for the day one challenge.

Given the input as a string, it splits by lines and then in a single pass loops through the items. As it sees each value it adds it to the current total for that elf and when it encounters an empty line it then marks that elf as finished by adding their total value to the heap.

The second argument corresponds to how many elves’ totals should be included when returning the total output. This means that part one can be solved by providing 1 and part two can be solved by providing 3.

§Example

let input = "1\n2\n\n3\n"; // probably read this from the input file...
assert_eq!(y22d01(input, 1), 3);
assert_eq!(y22d01(input, 2), 6);