Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @numba.njit
- def day14_slide_left(grid):
- for row in grid:
- open = 0
- for x, v in enumerate(row):
- if v == ord('.'):
- pass
- elif v == ord('O'):
- if open < x:
- row[open], row[x] = ord('O'), ord('.')
- open += 1
- else:
- open = x + 1
- def day14(s, *, part2=False, num=1_000_000_000):
- grid = np.array([[ord(ch) for ch in line] for line in s.splitlines()], np.uint8)
- grid = np.rot90(grid) # North is now left.
- grid = np.ascontiguousarray(grid) # Layout in C-order, for tobytes().
- if not part2:
- day14_slide_left(grid)
- else:
- configs: dict[Any, int] = {} # hashed_grid -> index.
- period = -1
- index = 0
- while True:
- if period < 0:
- if (prev_index := configs.setdefault(grid.tobytes(), index)) != index:
- period = index - prev_index
- print(f'At {index=}, found cycle with {period=}.')
- index = num - (num - index) % period
- if index == num:
- break
- for _ in range(4):
- day14_slide_left(grid)
- grid = np.rot90(grid, -1)
- index += 1
- return sum(len(grid[0]) - x for _, x in np.argwhere(grid == ord('O')))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement