Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day21_part2(s, nsteps=26_501_365):
- grid = np.array([list(line) for line in s.splitlines()])
- n = len(grid)
- nrings, remainder = divmod(nsteps, n)
- assert grid.shape[1] == n and grid[n // 2, n // 2] == 'S' and remainder == n // 2
- radius = 2
- grid = np.tile(grid, (1 + 2 * radius,) * 2)
- empty = grid != '#'
- active = np.full(np.array(grid.shape) + 2, False)
- active[(1 + radius * n + n // 2,) * 2] = True
- for _ in range(n // 2 + radius * n):
- active[1:-1, 1:-1] = empty & (
- active[:-2, 1:-1] | active[2:, 1:-1] | active[1:-1, :-2] | active[1:-1, 2:]
- )
- active = active[1:-1, 1:-1]
- counts = active.reshape(active.shape[0] // n, n, active.shape[1] // n, n).sum((1, 3))
- return (
- counts[2, 2] * (1 + 4 * (nrings // 2) * (nrings // 2 - 1))
- + counts[1, 2] * 4 * (nrings // 2) ** 2
- + (counts[0, 1] + counts[0, 3] + counts[4, 1] + counts[4, 3]) * nrings
- + (counts[1, 1] + counts[1, 3] + counts[3, 1] + counts[3, 3]) * (nrings - 1)
- + (counts[0, 2] + counts[2, 0] + counts[2, 4] + counts[4, 2])
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement