From aa6617eda20af145699894fbc588f9bdec1874a5 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 4 Dec 2021 13:34:59 +0000 Subject: day 4: replace loops with list.index --- 4.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/4.py b/4.py index 41dc053..ca63510 100644 --- a/4.py +++ b/4.py @@ -1,4 +1,4 @@ -from itertools import product, chain +from itertools import chain from utils import open_day class Board: @@ -21,17 +21,17 @@ class Board: self.cells.append(cell) self.has_bingo = False def call(self, num: int): - 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 + 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 @staticmethod def from_string(s: str) -> 'Board': return Board([[int(n) for n in l.split()] for l in s.split('\n')]) -- cgit v1.2.3-54-g00ecf