From 80b190c164cf08d83ba8efb91bfcf556f9cc71f1 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 4 Dec 2021 14:25:40 +0000 Subject: day 4: only calculate unmarked_sum when needed --- 4.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/4.py b/4.py index e85f358..c75706c 100644 --- a/4.py +++ b/4.py @@ -4,7 +4,6 @@ class Board: col_hits: list[int] row_hits: list[int] has_bingo: bool - unmarked_sum: int nums: dict[int, tuple[int, int]] def __init__(self, cells: list[list[int]]) -> None: width: int = len(cells[0]) @@ -15,19 +14,20 @@ class Board: for y, row in enumerate(cells): for x, cell in enumerate(row): self.nums[cell] = (x, y) - self.unmarked_sum = sum(self.nums.keys()) self.has_bingo = False def call(self, num: int) -> None: - pos: tuple[int, int] = self.nums.get(num) + pos: tuple[int, int] | None = self.nums.pop(num, None) if pos is None: return self.col_hits[pos[0]] -= 1 self.row_hits[pos[1]] -= 1 - self.unmarked_sum -= num self.has_bingo = ( self.has_bingo or not self.col_hits[pos[0]] or not self.row_hits[pos[1]] ) + @property + def unmarked_sum(self) -> int: + return sum(self.nums.keys()) @staticmethod def from_string(s: str) -> 'Board': return Board([[int(n) for n in l.split()] for l in s.split('\n')]) -- cgit v1.2.3-54-g00ecf