summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-04 13:34:59 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-04 13:34:59 +0000
commitaa6617eda20af145699894fbc588f9bdec1874a5 (patch)
tree24d268cf07c584d572eca1731e85ebb2544c6b04
parent13be876c7a1b497e4b4b765fadc4d832844ad24a (diff)
downloadaoc2021-aa6617eda20af145699894fbc588f9bdec1874a5.tar.gz
aoc2021-aa6617eda20af145699894fbc588f9bdec1874a5.tar.xz
aoc2021-aa6617eda20af145699894fbc588f9bdec1874a5.zip
day 4: replace loops with list.index
-rw-r--r--4.py24
1 files 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')])