Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day10(s, *, part2=False, offset=(1, -1)): # Offset must be set manually as (+-1, +-1).
- grid = np.array([list(line) for line in s.splitlines()])
- (yx_start,) = np.argwhere(grid == 'S')
- loop = [tuple(yx_start)]
- dy, dx = 1, 0
- yx = tuple(yx_start + (dy, dx))
- while (ch := grid[yx]) != 'S':
- loop.append(yx)
- dy, dx = (dx, dy) if ch in 'L7' else (-dx, -dy) if ch in 'JF' else (dy, dx)
- yx = yx[0] + dy, yx[1] + dx
- if not part2:
- return len(loop) // 2
- grid2 = np.full(np.array(grid.shape) * 2 + 1, 0)
- for (y0, x0), (y1, x1) in itertools.pairwise(loop + [loop[0]]):
- grid2[y0 * 2, x0 * 2] = grid2[y0 + y1, x0 + x1] = 1
- seed_point = tuple(yx_start * 2 + offset)
- skimage.segmentation.flood_fill(grid2, seed_point, 2, in_place=True)
- return np.count_nonzero(grid2[::2, ::2] == 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement