diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 14:25:40 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 14:25:40 +0000 |
commit | 80b190c164cf08d83ba8efb91bfcf556f9cc71f1 (patch) | |
tree | 7e8cd45ce98791e2cc19bab6bc118488b76fbe89 | |
parent | 31995dca1da963471328125884989a53a535bffd (diff) | |
download | aoc2021-80b190c164cf08d83ba8efb91bfcf556f9cc71f1.tar.gz aoc2021-80b190c164cf08d83ba8efb91bfcf556f9cc71f1.tar.xz aoc2021-80b190c164cf08d83ba8efb91bfcf556f9cc71f1.zip |
day 4: only calculate unmarked_sum when needed
-rw-r--r-- | 4.py | 8 |
1 files changed, 4 insertions, 4 deletions
@@ -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')]) |