Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def grid_from_string(s):
- return np.array(list(map(list, s.strip('\n').split('\n'))))
- def count_steady_state_occupied1(s):
- grid = grid_from_string(s)
- shape = grid.shape
- while True:
- prev = grid.copy()
- for y in range(shape[0]):
- for x in range(shape[1]):
- num_adjacent_occupied = 0
- for y2 in range(max(0, y - 1), min(shape[0], y + 2)):
- for x2 in range(max(0, x - 1), min(shape[1], x + 2)):
- num_adjacent_occupied += ((y2 != y or x2 != x) and
- prev[y2, x2] == '#')
- if prev[y, x] == 'L' and num_adjacent_occupied == 0:
- grid[y, x] = '#'
- elif prev[y, x] == '#' and num_adjacent_occupied >= 4:
- grid[y, x] = 'L'
- if np.all(grid == prev):
- return np.sum(grid == '#')
- def count_steady_state_occupied2(s):
- grid = grid_from_string(s)
- shape = grid.shape
- while True:
- prev = grid.copy()
- for y in range(shape[0]):
- for x in range(shape[1]):
- num_visible_occupied = 0
- for dyx in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1),
- (1, 0), (1, 1)):
- yx = (y, x)
- while True:
- yx = (yx[0] + dyx[0], yx[1] + dyx[1])
- if not (0 <= yx[0] < shape[0] and 0 <= yx[1] < shape[1]):
- break
- if prev[yx] != '.':
- num_visible_occupied += prev[yx] == '#'
- break
- if prev[y, x] == 'L' and num_visible_occupied == 0:
- grid[y, x] = '#'
- elif prev[y, x] == '#' and num_visible_occupied >= 5:
- grid[y, x] = 'L'
- if np.all(grid == prev):
- return np.sum(grid == '#')
Advertisement
Add Comment
Please, Sign In to add comment