diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 16:58:58 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 16:58:58 +0000 |
commit | 351972ca3092e11b436ff629aa4734b055aa4f0c (patch) | |
tree | e09095ba9757d9811707b0b4b6a00d4bd3b34714 | |
parent | 73189920ac73087350de99495877cb2a902ddff2 (diff) | |
download | aoc2021-351972ca3092e11b436ff629aa4734b055aa4f0c.tar.gz aoc2021-351972ca3092e11b436ff629aa4734b055aa4f0c.tar.xz aoc2021-351972ca3092e11b436ff629aa4734b055aa4f0c.zip |
day 4: more improvements
-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) |