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 __future__ import annotations
from typing import Iterator, Generator from typing import Iterator
day_num = 1 day_num = 1
def part1(lines: Iterator[str]) -> int: def part1(lines: Iterator[str]) -> int:
return max(parse_lines(lines)) return max(parse_carried_food(lines))
def part2(lines: Iterator[str]) -> int: 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 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 Returns
------- -------
Generator[int, None, None] Iterator[int]
A generator for the sum of each block An iterator for the calories carried by each elf
Raises Raises
------ ------
ValueError ValueError
If a line is neither empty nor represents a valid int If a line is neither empty nor represents a valid int
""" """
current = 0
calories = 0
for line in lines: for line in lines:
if line: if line:
current += int(line) calories += int(line)
else: else:
yield current yield calories
current = 0 calories = 0
if current != 0: if calories != 0:
yield current yield calories