Advertisement
hhoppe

Advent of code 2023 day 10

Dec 10th, 2023 (edited)
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. def day10(s, *, part2=False):
  2.   grid = np.array([list(line) for line in s.splitlines()])
  3.   ((y, x),) = np.argwhere(grid == 'S')
  4.   loop = []
  5.   dy, dx = 1, 0  # The start had a downward loop arm in all examples I saw.
  6.   while (ch := grid[y, x]) != 'S' or not loop:
  7.     loop.append((y, x))
  8.     dy, dx = (dx, dy) if ch in 'L7' else (-dx, -dy) if ch in 'JF' else (dy, dx)
  9.     y, x = y + dy, x + dx
  10.  
  11.   if not part2:
  12.     return len(loop) // 2
  13.  
  14.   grid2 = np.full(np.array(grid.shape) * 2 + 1, 1)
  15.   for (y0, x0), (y1, x1) in itertools.pairwise(loop + [loop[0]]):
  16.     grid2[y0 * 2 + 1, x0 * 2 + 1] = grid2[y0 + y1 + 1, x0 + x1 + 1] = 0
  17.  
  18.   skimage.segmentation.flood_fill(grid2, (0, 0), 0, in_place=True)
  19.   return np.count_nonzero(grid2[1::2, 1::2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement