diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 16:42:10 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 16:42:10 +0000 |
commit | 73189920ac73087350de99495877cb2a902ddff2 (patch) | |
tree | a137d8acb21f03098871acf01423c036a394d6f2 | |
parent | f57a4810e22d446970c19286f8ebae4c9afa24ba (diff) | |
download | aoc2021-73189920ac73087350de99495877cb2a902ddff2.tar.gz aoc2021-73189920ac73087350de99495877cb2a902ddff2.tar.xz aoc2021-73189920ac73087350de99495877cb2a902ddff2.zip |
day 4: small performance improvement
-rw-r--r-- | 4.py | 13 |
1 files changed, 8 insertions, 5 deletions
@@ -34,16 +34,19 @@ class Board: return Board([[int(n) for n in l.split()] for l in s.split('\n')]) def solve(nums: list[int], boards: list[Board]) -> tuple[int, int]: + indices: set[int] = { i for i in range(len(boards)) } wins: list[int] = [] num: int for num in nums: i: int board: Board - for i, board in enumerate(boards): - if board.has_bingo: continue - board.call(num) - if board.has_bingo: - wins.append(num * board.unmarked_sum) + winners: set[int] = set() + for i in indices: + boards[i].call(num) + if boards[i].has_bingo: + wins.append(num * boards[i].unmarked_sum) + winners.add(i) + indices.difference_update(winners) return wins[0], wins[-1] nums: str | list[int] |