Advertisement
Guest User

AoC 2022 Day 24 - Python

a guest
Dec 24th, 2022
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. def parse(data: str):
  2.     rows = data.split("\n")
  3.     bottom_wall = len(rows) - 2
  4.     right_wall = len(rows[0]) - 2
  5.     up, down, left, right = [], [], [], []
  6.     for i, row in enumerate(rows):
  7.         for j, spot in enumerate(row):
  8.             if spot == '^':
  9.                 up.append(complex(j-1, i-1))
  10.             elif spot == 'v':
  11.                 down.append(complex(j-1, i-1))
  12.             elif spot == '<':
  13.                 left.append(complex(j-1, i-1))
  14.             elif spot == '>':
  15.                 right.append(complex(j-1, i-1))
  16.     return up, down, left, right, bottom_wall, right_wall
  17.  
  18. def move_blizzard(up: list[complex], down: list[complex], left: list[complex], right: list[complex], right_wall: int, bottom_wall: int):
  19.     for i, b in enumerate(up):
  20.         b -= 1j
  21.         if b.imag < 0:
  22.             b += complex(0, bottom_wall)
  23.         up[i] = b
  24.     for i, b in enumerate(down):
  25.         b += 1j
  26.         if b.imag >= bottom_wall:
  27.             b = b.real
  28.         down[i] = b
  29.     for i, b in enumerate(left):
  30.         b -= 1
  31.         if b.real < 0:
  32.             b += right_wall
  33.         left[i] = b
  34.     for i, b in enumerate(right):
  35.         b += 1
  36.         if b.real >= right_wall:
  37.             b -= right_wall
  38.         right[i] = b
  39.  
  40. def print_blizzard(up: list[complex], down: list[complex], left: list[complex], right: list[complex], right_wall: int, bottom_wall: int, queue: list[complex]):
  41.     # print_blizzard(up, down, left, right, right_wall, bottom_wall)
  42.     out = [['.']*right_wall for _ in range(bottom_wall)]
  43.     for b in up:
  44.         out[int(b.imag)][int(b.real)] = '^'
  45.     for b in down:
  46.         out[int(b.imag)][int(b.real)] = 'v'
  47.     for b in left:
  48.         out[int(b.imag)][int(b.real)] = '<'
  49.     for b in right:
  50.         out[int(b.imag)][int(b.real)] = '>'
  51.     out = [['#'] + row + ['#'] for row in out]
  52.     out.insert(0, ['#', '.'] + ['#']*right_wall)
  53.     out.append(['#']*right_wall + ['.', '#'])
  54.     for you in queue:
  55.         out[int(you.imag)+1][int(you.real)+1] = 'E'
  56.     for row in out:
  57.         print(''.join(row))
  58.  
  59. def blizzard(data: str):
  60.     up, down, left, right, bottom_wall, right_wall = parse(data)
  61.     queue = [-1j]
  62.     moves = 0
  63.     destinations = [-1j, complex(right_wall-1, bottom_wall)]
  64.     destination = complex(right_wall-1, bottom_wall)
  65.     wall = set()
  66.     for i in range(-1, right_wall+1):
  67.         wall.add(complex(i, -1))
  68.         wall.add(complex(i, bottom_wall))
  69.     for i in range(-1, bottom_wall+1):
  70.         wall.add(complex(-1, i))
  71.         wall.add(complex(right_wall, i))
  72.     wall.add(-2j)
  73.     wall.add(complex(right_wall-1, bottom_wall+1))
  74.     wall -= set([-1j, complex(right_wall-1, bottom_wall)])
  75.     while True:
  76.         move_blizzard(up, down, left, right, right_wall, bottom_wall)
  77.         next_queue = set()
  78.         moves += 1
  79.         blizzards = set(up+down+left+right).union(wall)
  80.         while len(queue):
  81.             you = queue.pop(0)
  82.             if you == destination:
  83.                 print(moves - 1)
  84.                 if len(destinations):
  85.                     destination = destinations.pop(0)
  86.                     queue = []
  87.                     next_queue = set()
  88.                 else:
  89.                     return
  90.             for i in [0, 1, -1, 1j, -1j]:
  91.                 move = you + i
  92.                 if move not in blizzards:
  93.                     next_queue.add(move)
  94.         queue = list(next_queue)
  95.  
  96. import time
  97. st = time.time()
  98.  
  99. # blizzard(example)
  100. blizzard(input)
  101. print(time.time() - st)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement