day11/python: optimizations

This commit is contained in:
Xiretza 2020-12-13 18:31:09 +01:00
parent 29a768fdf8
commit 7c5dc39073

View file

@ -57,20 +57,17 @@ class Layout:
return len(new_tiles) != 0 return len(new_tiles) != 0
def _find_neighbour(self, row: int, col: int, direction: Tuple[int, int]) -> Optional[Tuple[int, int]]: def _find_neighbour(self, row: int, col: int, direction: Tuple[int, int]) -> Optional[Tuple[int, int]]:
curr_row = row
curr_col = col
steps = 0 steps = 0
while self.max_neighbour_steps is None or steps < self.max_neighbour_steps: while self.max_neighbour_steps is None or steps < self.max_neighbour_steps:
curr_row += direction[0] row += direction[0]
curr_col += direction[1] col += direction[1]
if curr_row < 0 or curr_row >= self.num_rows: if row < 0 or row >= self.num_rows:
break break
if curr_col < 0 or curr_col >= self.num_cols: if col < 0 or col >= self.num_cols:
break break
if self.get(curr_row, curr_col) != Tile.FLOOR: if self.get(row, col) != Tile.FLOOR:
return (curr_row, curr_col) return (row, col)
steps += 1 steps += 1
return None return None
@ -89,7 +86,7 @@ class Layout:
yield self.get(nrow, ncol) yield self.get(nrow, ncol)
def count_neighbours(self, row: int, col: int) -> int: 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: def run(layout: Layout) -> int: