day11/python: optimizations

This commit is contained in:
Xiretza 2020-12-13 18:31:09 +01:00
parent 4b5d09f5df
commit 46fb1147fb
Signed by: xiretza
GPG Key ID: 17B78226F7139993
1 changed files with 7 additions and 10 deletions

View File

@ -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: