day01 tweak

This commit is contained in:
Ruediger Ludwig 2022-12-01 17:56:30 +01:00
parent a607fbb329
commit 86dab16b47

View file

@ -1,19 +1,19 @@
from __future__ import annotations
from typing import Iterator, Generator
from typing import Iterator
day_num = 1
def part1(lines: Iterator[str]) -> int:
return max(parse_lines(lines))
return max(parse_carried_food(lines))
def part2(lines: Iterator[str]) -> int:
return sum(sorted(parse_lines(lines))[-3:])
return sum(sorted(parse_carried_food(lines))[-3:])
def parse_lines(lines: Iterator[str]) -> Generator[int, None, None]:
def parse_carried_food(lines: Iterator[str]) -> Iterator[int]:
"""
Parses lines of string as integers and returns the sum of blocks separated by blank lines
@ -24,22 +24,22 @@ def parse_lines(lines: Iterator[str]) -> Generator[int, None, None]:
Returns
-------
Generator[int, None, None]
A generator for the sum of each block
Iterator[int]
An iterator for the calories carried by each elf
Raises
------
ValueError
If a line is neither empty nor represents a valid int
"""
current = 0
calories = 0
for line in lines:
if line:
current += int(line)
calories += int(line)
else:
yield current
current = 0
yield calories
calories = 0
if current != 0:
yield current
if calories != 0:
yield calories