Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import scipy as sp
- trans = {">": 0, "<": 1, "v": 2, "^": 3}
- with open("u") as fp:
- lines = fp.read().strip().split("\n")
- lines = lines[1:-1]
- ymax = len(lines)
- xmax = len(lines[0]) - 2
- winds = np.zeros((4, ymax, xmax), dtype=bool)
- for y, line in enumerate(lines):
- line = line.strip("#")
- for x, c in enumerate(line):
- if c in trans:
- winds[trans[c]][y][x] = 1
- def rollwinds(winds):
- winds[0] = np.roll(winds[0], 1, axis=1)
- winds[1] = np.roll(winds[1], -1, axis=1)
- winds[2] = np.roll(winds[2], 1, axis=0)
- winds[3] = np.roll(winds[3], -1, axis=0)
- return winds
- cross_kernel = np.array(
- [[0, 1, 0], [1, 1, 1], [0, 1, 0]],
- dtype=bool,
- )
- minute = 0
- path = np.zeros((ymax, xmax), dtype=bool)
- while path[-1][-1] == False: # as long as next isnt last minute
- minute += 1
- winds = rollwinds(winds)
- # just dilate all the possible positions
- path = sp.ndimage.binary_dilation(
- path,
- cross_kernel,
- )
- path[0][0] = True # could have always waited (always seed start)
- save_positions = np.any(winds, axis=0)
- path = np.bitwise_and(path, np.invert(save_positions))
- path = np.zeros((ymax, xmax), dtype=bool)
- while path[0][0] == False:
- minute += 1
- winds = rollwinds(winds)
- path = sp.ndimage.binary_dilation(
- path,
- cross_kernel,
- )
- path[-1][-1] = True
- save_positions = np.invert(np.any(winds, axis=0))
- path = np.bitwise_and(path, save_positions)
- path = np.zeros((ymax, xmax), dtype=bool)
- while path[-1][-1] == False:
- minute += 1
- winds = rollwinds(winds)
- path = sp.ndimage.binary_dilation(
- path,
- cross_kernel,
- )
- path[0][0] = True
- save_positions = np.any(winds, axis=0)
- path = np.bitwise_and(path, np.invert(save_positions))
- print(minute + 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement