Advertisement
hhoppe

Advent of code 2023 day 21

Dec 21st, 2023 (edited)
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. def day21_part2(s, nsteps=26_501_365):
  2.   grid = np.array([list(line) for line in s.splitlines()])
  3.   n = len(grid)
  4.   nrings, remainder = divmod(nsteps, n)
  5.   assert grid.shape[1] == n and grid[n // 2, n // 2] == 'S' and remainder == n // 2
  6.   radius = 2
  7.   grid = np.tile(grid, (1 + 2 * radius,) * 2)
  8.   empty = grid != '#'
  9.   active = np.full(np.array(grid.shape) + 2, False)
  10.   active[(1 + radius * n + n // 2,) * 2] = True
  11.   for _ in range(n // 2 + radius * n):
  12.     active[1:-1, 1:-1] = empty & (
  13.         active[:-2, 1:-1] | active[2:, 1:-1] | active[1:-1, :-2] | active[1:-1, 2:]
  14.     )
  15.   active = active[1:-1, 1:-1]
  16.   counts = active.reshape(active.shape[0] // n, n, active.shape[1] // n, n).sum((1, 3))
  17.   return (
  18.       counts[2, 2] * (1 + 4 * (nrings // 2) * (nrings // 2 - 1))
  19.       + counts[1, 2] * 4 * (nrings // 2) ** 2
  20.       + (counts[0, 1] + counts[0, 3] + counts[4, 1] + counts[4, 3]) * nrings
  21.       + (counts[1, 1] + counts[1, 3] + counts[3, 1] + counts[3, 3]) * (nrings - 1)
  22.       + (counts[0, 2] + counts[2, 0] + counts[2, 4] + counts[4, 2])
  23.   )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement