Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day10(s, *, part2=False):
- grid = np.array([list(line) for line in s.splitlines()])
- ((y, x),) = np.argwhere(grid == 'S')
- loop = []
- dy, dx = 1, 0 # The start had a downward loop arm in all examples I saw.
- while (ch := grid[y, x]) != 'S' or not loop:
- loop.append((y, x))
- dy, dx = (dx, dy) if ch in 'L7' else (-dx, -dy) if ch in 'JF' else (dy, dx)
- y, x = y + dy, x + dx
- if not part2:
- return len(loop) // 2
- grid2 = np.full(np.array(grid.shape) * 2 + 1, 1)
- for (y0, x0), (y1, x1) in itertools.pairwise(loop + [loop[0]]):
- grid2[y0 * 2 + 1, x0 * 2 + 1] = grid2[y0 + y1 + 1, x0 + x1 + 1] = 0
- skimage.segmentation.flood_fill(grid2, (0, 0), 0, in_place=True)
- return np.count_nonzero(grid2[1::2, 1::2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement