From 7c5dc39073af34f326f4870e2618a72483c395a9 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sun, 13 Dec 2020 18:31:09 +0100 Subject: [PATCH] day11/python: optimizations --- 2020/day11/day11.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/2020/day11/day11.py b/2020/day11/day11.py index 79a8e2e..1e3b29b 100644 --- a/2020/day11/day11.py +++ b/2020/day11/day11.py @@ -57,20 +57,17 @@ class Layout: return len(new_tiles) != 0 def _find_neighbour(self, row: int, col: int, direction: Tuple[int, int]) -> Optional[Tuple[int, int]]: - curr_row = row - curr_col = col - steps = 0 while self.max_neighbour_steps is None or steps < self.max_neighbour_steps: - curr_row += direction[0] - curr_col += direction[1] - if curr_row < 0 or curr_row >= self.num_rows: + row += direction[0] + col += direction[1] + if row < 0 or row >= self.num_rows: break - if curr_col < 0 or curr_col >= self.num_cols: + if col < 0 or col >= self.num_cols: break - if self.get(curr_row, curr_col) != Tile.FLOOR: - return (curr_row, curr_col) + if self.get(row, col) != Tile.FLOOR: + return (row, col) steps += 1 return None @@ -89,7 +86,7 @@ class Layout: yield self.get(nrow, ncol) def count_neighbours(self, row: int, col: int) -> int: - return len([tile for tile in self.neighbours(row, col) if tile == Tile.OCCUPIED]) + return sum(1 for tile in self.neighbours(row, col) if tile == Tile.OCCUPIED) def run(layout: Layout) -> int: