Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @numba.njit
- def min_time_at_dst(grid, time, start_yx, dst_yx):
- shape = grid.shape
- active = {start_yx}
- max_time = time + shape[0] + shape[1] + 150 # 113 is min for my puzzle input.
- while active:
- new_active = set()
- time += 1
- for y, x in active:
- for dy, dx in ((1, 0), (0, 1), (0, 0), (-1, 0), (0, -1)):
- yx2 = y2, x2 = y + dy, x + dx
- if yx2 == dst_yx:
- return time
- if (0 <= y2 < shape[0] and 0 <= x2 < shape[1] and
- grid[y2, (x2 - time) % shape[1]] != '>' and
- grid[y2, (x2 + time) % shape[1]] != '<' and
- grid[(y2 - time) % shape[0], x2] != 'v' and
- grid[(y2 + time) % shape[0], x2] != '^') or yx2 == start_yx:
- remaining_dist = abs(y2 - dst_yx[0]) + abs(x2 - dst_yx[1])
- if time + remaining_dist <= max_time:
- new_active.add(yx2)
- active = new_active
- raise AssertionError('No path found; try increasing max_time.')
- def day24(s, *, part2=False):
- grid = np.array([list(line) for line in s.splitlines()])
- grid = grid[1:-1, 1:-1] # (5, 5) or (35, 100); lcm=700 is large.
- start, final = (-1, 0), (grid.shape[0], grid.shape[1] - 1)
- time1 = min_time_at_dst(grid, 0, start, final)
- if not part2:
- return time1
- time2 = min_time_at_dst(grid, time1, final, start)
- time3 = min_time_at_dst(grid, time2, start, final)
- return time3 # time1 = 225, time2 = 463, time3 = 711
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement