Advertisement
Guest User

Untitled

a guest
Dec 24th, 2022
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. import numpy as np
  2. import scipy as sp
  3.  
  4.  
  5. trans = {">": 0, "<": 1, "v": 2, "^": 3}
  6.  
  7. with open("u") as fp:
  8.     lines = fp.read().strip().split("\n")
  9.     lines = lines[1:-1]
  10.     ymax = len(lines)
  11.     xmax = len(lines[0]) - 2
  12.     winds = np.zeros((4, ymax, xmax), dtype=bool)
  13.     for y, line in enumerate(lines):
  14.         line = line.strip("#")
  15.         for x, c in enumerate(line):
  16.             if c in trans:
  17.                 winds[trans[c]][y][x] = 1
  18.  
  19.  
  20. def rollwinds(winds):
  21.     winds[0] = np.roll(winds[0], 1, axis=1)
  22.     winds[1] = np.roll(winds[1], -1, axis=1)
  23.     winds[2] = np.roll(winds[2], 1, axis=0)
  24.     winds[3] = np.roll(winds[3], -1, axis=0)
  25.     return winds
  26.  
  27.  
  28. cross_kernel = np.array(
  29.     [[0, 1, 0], [1, 1, 1], [0, 1, 0]],
  30.     dtype=bool,
  31. )
  32.  
  33. minute = 0
  34. path = np.zeros((ymax, xmax), dtype=bool)
  35. while path[-1][-1] == False:  # as long as next isnt last minute
  36.     minute += 1
  37.     winds = rollwinds(winds)
  38.     # just dilate all the possible positions
  39.     path = sp.ndimage.binary_dilation(
  40.         path,
  41.         cross_kernel,
  42.     )
  43.     path[0][0] = True  # could have always waited (always seed start)
  44.     save_positions = np.any(winds, axis=0)
  45.     path = np.bitwise_and(path, np.invert(save_positions))
  46.  
  47. path = np.zeros((ymax, xmax), dtype=bool)
  48. while path[0][0] == False:
  49.     minute += 1
  50.     winds = rollwinds(winds)
  51.     path = sp.ndimage.binary_dilation(
  52.         path,
  53.         cross_kernel,
  54.     )
  55.     path[-1][-1] = True
  56.     save_positions = np.invert(np.any(winds, axis=0))
  57.     path = np.bitwise_and(path, save_positions)
  58.  
  59. path = np.zeros((ymax, xmax), dtype=bool)
  60. while path[-1][-1] == False:
  61.     minute += 1
  62.     winds = rollwinds(winds)
  63.     path = sp.ndimage.binary_dilation(
  64.         path,
  65.         cross_kernel,
  66.     )
  67.     path[0][0] = True
  68.     save_positions = np.any(winds, axis=0)
  69.     path = np.bitwise_and(path, np.invert(save_positions))
  70.  
  71. print(minute + 1)
  72.  
Tags: aoc2022-24
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement