Advertisement
hhoppe

Advent of code 2022 day 24

Dec 24th, 2022 (edited)
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. @numba.njit
  2. def min_time_at_dst(grid, time, start_yx, dst_yx):
  3.   shape = grid.shape
  4.   active = {start_yx}
  5.   max_time = time + shape[0] + shape[1] + 150  # 113 is min for my puzzle input.
  6.  
  7.   while active:
  8.     new_active = set()
  9.     time += 1
  10.     for y, x in active:
  11.       for dy, dx in ((1, 0), (0, 1), (0, 0), (-1, 0), (0, -1)):
  12.         yx2 = y2, x2 = y + dy, x + dx
  13.         if yx2 == dst_yx:
  14.           return time
  15.         if (0 <= y2 < shape[0] and 0 <= x2 < shape[1] and
  16.             grid[y2, (x2 - time) % shape[1]] != '>' and
  17.             grid[y2, (x2 + time) % shape[1]] != '<' and
  18.             grid[(y2 - time) % shape[0], x2] != 'v' and
  19.             grid[(y2 + time) % shape[0], x2] != '^') or yx2 == start_yx:
  20.           remaining_dist = abs(y2 - dst_yx[0]) + abs(x2 - dst_yx[1])
  21.           if time + remaining_dist <= max_time:
  22.             new_active.add(yx2)
  23.     active = new_active
  24.  
  25.   raise AssertionError('No path found; try increasing max_time.')
  26.  
  27.  
  28. def day24(s, *, part2=False):
  29.   grid = np.array([list(line) for line in s.splitlines()])
  30.   grid = grid[1:-1, 1:-1]  # (5, 5) or (35, 100); lcm=700 is large.
  31.   start, final = (-1, 0), (grid.shape[0], grid.shape[1] - 1)
  32.  
  33.   time1 = min_time_at_dst(grid, 0, start, final)
  34.   if not part2:
  35.     return time1
  36.   time2 = min_time_at_dst(grid, time1, final, start)
  37.   time3 = min_time_at_dst(grid, time2, start, final)
  38.   return time3  # time1 = 225, time2 = 463, time3 = 711
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement