From 351972ca3092e11b436ff629aa4734b055aa4f0c Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 4 Dec 2021 16:58:58 +0000 Subject: day 4: more improvements --- 4.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/4.py b/4.py index f2c3063..0c7cf4f 100644 --- a/4.py +++ b/4.py @@ -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) -- cgit v1.2.3-54-g00ecf