Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day14(s, *, part2=False, num=1_000_000_000):
- grid = np.rot90(np.array([list(line) for line in s.splitlines()])) # North is left.
- def slide_left():
- for row in grid:
- open = 0
- for x, ch in enumerate(row):
- if ch == '.':
- pass
- elif ch == 'O':
- if open < x:
- row[open], row[x] = 'O', '.'
- open += 1
- else:
- open = x + 1
- if not part2:
- slide_left()
- 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):
- slide_left()
- grid = np.rot90(grid, -1)
- index += 1
- return sum(len(grid[0]) - x for _, x in np.argwhere(grid == 'O'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement