Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day10(s, *, part2=False): # Interior test using row intersection counting.
- grid = np.array([list(line) for line in s.splitlines()])
- ((y, x),) = np.argwhere(grid == 'S')
- loop: list[tuple[int, int]] = []
- dy, dx = 1, 0 # The start has a downward loop arm in all the examples I see.
- 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
- loop_mask = np.full(grid.shape, False)
- loop_mask[tuple(np.array(loop).T)] = True
- lookup = np.array([chr(i) in '|LJ' for i in range(256)])
- is_wall = lookup[grid.view(np.uint32)] # ('U1' and uint32 have the same bitwidth.)
- is_wall[~loop_mask] = 0
- count = is_wall.cumsum(1) % 2
- count[loop_mask] = 0
- return count.sum()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement