summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-04 16:58:58 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-04 16:58:58 +0000
commit351972ca3092e11b436ff629aa4734b055aa4f0c (patch)
treee09095ba9757d9811707b0b4b6a00d4bd3b34714
parent73189920ac73087350de99495877cb2a902ddff2 (diff)
downloadaoc2021-351972ca3092e11b436ff629aa4734b055aa4f0c.tar.gz
aoc2021-351972ca3092e11b436ff629aa4734b055aa4f0c.tar.xz
aoc2021-351972ca3092e11b436ff629aa4734b055aa4f0c.zip
day 4: more improvements
-rw-r--r--4.py10
1 files changed, 9 insertions, 1 deletions
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)