Advertisement
hhoppe

Advent of code 2023 day 10 initial

Dec 10th, 2023 (edited)
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. def day10(s, *, part2=False, offset=(1, -1)):  # Offset must be set manually as (+-1, +-1).
  2.   grid = np.array([list(line) for line in s.splitlines()])
  3.   (yx_start,) = np.argwhere(grid == 'S')
  4.   loop = [tuple(yx_start)]
  5.   dy, dx = 1, 0
  6.   yx = tuple(yx_start + (dy, dx))
  7.   while (ch := grid[yx]) != 'S':
  8.     loop.append(yx)
  9.     dy, dx = (dx, dy) if ch in 'L7' else (-dx, -dy) if ch in 'JF' else (dy, dx)
  10.     yx = yx[0] + dy, yx[1] + dx
  11.  
  12.   if not part2:
  13.     return len(loop) // 2
  14.  
  15.   grid2 = np.full(np.array(grid.shape) * 2 + 1, 0)
  16.   for (y0, x0), (y1, x1) in itertools.pairwise(loop + [loop[0]]):
  17.     grid2[y0 * 2, x0 * 2] = grid2[y0 + y1, x0 + x1] = 1
  18.  
  19.   seed_point = tuple(yx_start * 2 + offset)
  20.   skimage.segmentation.flood_fill(grid2, seed_point, 2, in_place=True)
  21.   return np.count_nonzero(grid2[::2, ::2] == 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement