summaryrefslogtreecommitdiffstats
path: root/4.py
diff options
context:
space:
mode:
Diffstat (limited to '4.py')
-rw-r--r--4.py38
1 files changed, 17 insertions, 21 deletions
diff --git a/4.py b/4.py
index 5f15a8d..41dc053 100644
--- a/4.py
+++ b/4.py
@@ -1,3 +1,4 @@
+from itertools import product, chain
from utils import open_day
class Board:
@@ -11,35 +12,30 @@ class Board:
def __init__(self, cells: list[list[int]]):
self.width = len(cells[0])
self.height = len(cells)
- self.col_hits = [0 for _ in range(self.width)]
- self.row_hits = [0 for _ in range(self.height)]
+ self.col_hits = [0] * self.width
+ self.row_hits = [0] * self.height
self.unmarked_sum = 0
self.cells = []
- for row in cells:
- for cell in row:
- self.unmarked_sum += cell
- self.cells.append(cell)
+ for cell in chain.from_iterable(cells):
+ self.unmarked_sum += cell
+ self.cells.append(cell)
self.has_bingo = False
- def check_bingo(self):
- self.has_bingo = (
- any(hits == self.height for hits in self.col_hits) or
- any(hits == self.width for hits in self.row_hits)
- )
def call(self, num: int):
- for y in range(self.height):
- for x in range(self.width):
- cell = self.cells[x + self.width * y]
- if cell == num:
- self.col_hits[x] += 1
- self.row_hits[y] += 1
- self.unmarked_sum -= cell
- self.check_bingo()
- return
+ for y, x in product(range(self.height), range(self.width)):
+ cell = self.cells[x + self.width * y]
+ if cell == num:
+ self.col_hits[x] += 1
+ self.row_hits[y] += 1
+ self.unmarked_sum -= cell
+ self.has_bingo = (
+ any(hits == self.height for hits in self.col_hits) or
+ any(hits == self.width for hits in self.row_hits)
+ )
+ return
@staticmethod
def from_string(s: str) -> '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]:
won: set[int] = set()
wins: list[int] = []