diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 13:23:45 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 13:23:45 +0000 |
commit | 13be876c7a1b497e4b4b765fadc4d832844ad24a (patch) | |
tree | 4b5d70684ca847940a89bc5ac132bc6dafde82e6 | |
parent | b2a89160ebe6e8ea5222c77a7416930dc332e949 (diff) | |
download | aoc2021-13be876c7a1b497e4b4b765fadc4d832844ad24a.tar.gz aoc2021-13be876c7a1b497e4b4b765fadc4d832844ad24a.tar.xz aoc2021-13be876c7a1b497e4b4b765fadc4d832844ad24a.zip |
day 4: a little bit cleaner
-rw-r--r-- | 4.py | 38 |
1 files changed, 17 insertions, 21 deletions
@@ -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] = [] |