diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 13:37:42 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-04 13:37:42 +0000 |
commit | 9de28ee17932d949a12381bf5a8de95b5ea7332b (patch) | |
tree | 291d58b410c68f10f2c99cf079941d9bd4d132b8 | |
parent | b19cad2bc7176063cba62d48df8b501db6e029d8 (diff) | |
download | aoc2021-9de28ee17932d949a12381bf5a8de95b5ea7332b.tar.gz aoc2021-9de28ee17932d949a12381bf5a8de95b5ea7332b.tar.xz aoc2021-9de28ee17932d949a12381bf5a8de95b5ea7332b.zip |
day 4: use a set of nums before list.index
-rw-r--r-- | 4.py | 23 |
1 files changed, 12 insertions, 11 deletions
@@ -9,6 +9,7 @@ class Board: row_hits: list[int] has_bingo: bool unmarked_sum: int + nums: set[int] def __init__(self, cells: list[list[int]]): self.width = len(cells[0]) self.height = len(cells) @@ -16,22 +17,22 @@ class Board: self.row_hits = [0] * self.height self.unmarked_sum = 0 self.cells = [] + self.nums = set() for cell in chain.from_iterable(cells): self.unmarked_sum += cell self.cells.append(cell) + self.nums.add(cell) self.has_bingo = False def call(self, num: int): - try: - pos: int = self.cells.index(num) - self.col_hits[pos // self.width] += 1 - self.row_hits[pos // self.height] += 1 - self.unmarked_sum -= num - self.has_bingo = ( - any(hits == self.height for hits in self.col_hits) or - any(hits == self.width for hits in self.row_hits) - ) - except ValueError: - pass + if num not in self.nums: return + pos: int = self.cells.index(num) + self.col_hits[pos // self.width] += 1 + self.row_hits[pos // self.height] += 1 + self.unmarked_sum -= num + self.has_bingo = ( + any(hits == self.height for hits in self.col_hits) or + any(hits == self.width for hits in self.row_hits) + ) @staticmethod def from_string(s: str) -> 'Board': return Board([[int(n) for n in l.split()] for l in s.split('\n')]) |