14.py
1 from lib import * 2 3 input = read_input(2022, 14) 4 5 6 rock = set() 7 maxy = 0 8 for line in input.splitlines(): 9 for a, b in sliding_window(line.split(" -> ")): 10 for x, y in iter_line(*map(int, a.split(",")), *map(int, b.split(","))): 11 rock.add((x, y)) 12 maxy = max(y, maxy) 13 14 15 def simulate(x, y, air, maxy): 16 while y <= maxy: 17 if air(x, y + 1): 18 y += 1 19 elif air(x - 1, y + 1): 20 x -= 1 21 y += 1 22 elif air(x + 1, y + 1): 23 x += 1 24 y += 1 25 else: 26 return x, y 27 return None 28 29 30 sand = set() 31 while s := simulate(500, 0, lambda x, y: (x, y) not in rock and (x, y) not in sand, maxy): 32 sand.add(s) 33 print(len(sand)) 34 35 36 sand = set() 37 while (500, 0) not in sand: 38 sand.add(simulate(500, 0, lambda x, y: y < maxy + 2 and (x, y) not in rock and (x, y) not in sand, maxy + 2)) 39 print(len(sand))