Advertisement
Guest User

Untitled

a guest
Dec 15th, 2022
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. from typing import Iterator
  2.  
  3. from aoclib import show_answers, get_puzzle_input
  4. import numpy as np
  5.  
  6. def solutions() -> Iterator[int]:
  7.     rock_paths = [[list(map(int, coord.split(","))) for coord in line.split(" -> ")] \
  8.                  for line in get_puzzle_input(day=14)]
  9.     size = (200,750)
  10.     cave_data = np.full(size,False)
  11.     y_max = 0
  12.     for rock_path in rock_paths:
  13.         for (c1,r1),(c2,r2) in zip(rock_path,rock_path[1:]):
  14.             (r1,r2),(c1,c2) = sorted((r1,r2)),sorted((c1,c2))
  15.             y_max = max(y_max,r2)
  16.             cave_data[r1:r2+1, c1:c2+1] = True
  17.     cave_data[y_max+2,:]=True
  18.     obsticles = set(zip(*np.where(cave_data)))
  19.     init_size = len(obsticles)
  20.     sand_motions = [(1,0),(1,-1),(1,1)]
  21.     has_not_reached_the_floor = True
  22.     moving_sand = [(0,500)]
  23.     while moving_sand:
  24.         while True:
  25.             ys,xs = moving_sand[-1]
  26.             for dy,dx in sand_motions:
  27.                 if not (c:=(ys+dy,xs+dx)) in obsticles:
  28.                     moving_sand.append(c)
  29.                     break
  30.             else:
  31.                 obsticles.add(moving_sand.pop(-1))
  32.                 break
  33.             if ys>=y_max and has_not_reached_the_floor:
  34.                 has_not_reached_the_floor = False
  35.                 yield len(obsticles)-init_size
  36.     yield len(obsticles)-init_size
  37.        
  38. show_answers(solutions())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement