Browse Source

day11/python: optimizations

main
Xiretza 1 year ago
parent
commit
46fb1147fb
Signed by: xiretza
GPG Key ID: 17B78226F7139993
  1. 17
      day11/day11.py

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

Loading…
Cancel
Save