diff options
-rw-r--r-- | 4.py | 10 |
1 files changed, 9 insertions, 1 deletions
@@ -1,3 +1,4 @@ +from collections import defaultdict from utils import open_day class Board: @@ -35,13 +36,20 @@ class Board: def solve(nums: list[int], boards: list[Board]) -> tuple[int, int]: indices: set[int] = { i for i in range(len(boards)) } + nums_indices: defaultdict[set[int]] = defaultdict(set) + i: int + board: Board + for i, board in enumerate(boards): + for n in board.nums: + nums_indices[n].add(i) wins: list[int] = [] num: int for num in nums: i: int board: Board winners: set[int] = set() - for i in indices: + num_indices: set[int] = nums_indices.get(num) + for i in indices & num_indices: boards[i].call(num) if boards[i].has_bingo: wins.append(num * boards[i].unmarked_sum) |