From 86dab16b4792335439212b2cf0ed226a59077b8d Mon Sep 17 00:00:00 2001 From: Ruediger Ludwig Date: Thu, 1 Dec 2022 17:56:30 +0100 Subject: [PATCH] day01 tweak --- advent/days/day01/solution.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/advent/days/day01/solution.py b/advent/days/day01/solution.py index 999ed48..4bb3a56 100644 --- a/advent/days/day01/solution.py +++ b/advent/days/day01/solution.py @@ -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