Guest User

Untitled

a guest
Dec 15th, 2022
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. from typing import Iterator
  2.  
  3. from aoclib import show_answers, get_puzzle_input
  4.  
  5. def solutions() -> Iterator[int]:
  6.     rock_paths = [[list(map(int,coord.split(","))) for coord in line.split("->")] for line in get_puzzle_input(day=14)]
  7.     obsticles = set()
  8.     y_floor = 0
  9.     for rock_path in rock_paths:
  10.         for (x1,y1),(x2,y2) in zip(rock_path,rock_path[1:]):
  11.             xrange = range(x1,x2+1) if x1<=x2 else range(x2,x1+1)
  12.             yrange,y_max = (range(y1,y2+1),y2) if y1<=y2 else (range(y2,y1+1),y1)
  13.             y_floor = max(y_floor,y_max+2)
  14.             obsticles |= set(complex(x,y) for x in xrange for y in yrange)
  15.     init_size = len(obsticles)
  16.     sand_steps = [1j,-1+1j,1+1j]
  17.     moving_sand = [500]
  18.     first_problem_solved = False
  19.     while moving_sand:
  20.         while True:
  21.             for dz in sand_steps:
  22.                 if not (c:=moving_sand[-1]+dz) in obsticles and c.imag<y_floor:
  23.                     moving_sand.append(c)
  24.                     break
  25.             else:
  26.                 obsticles.add(moving_sand.pop(-1))
  27.                 break
  28.             if moving_sand[-1].imag>y_floor-2 and not first_problem_solved:
  29.                 first_problem_solved = True
  30.                 yield len(obsticles)-init_size
  31.     yield len(obsticles)-init_size
  32.        
  33. show_answers(solutions())
Add Comment
Please, Sign In to add comment