+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
6e46de2
master
1from itertools import cycle 2 3shapes = ( 4 ((0 ,0 ), (1 ,0 ), (2 ,0 ), (3 ,0 )), 5 ((0 ,1 ), (1 ,0 ), (1 ,1 ), (1 ,2 ), (2 ,1 )), 6 ((0 ,0 ), (1 ,0 ), (2 ,0 ), (2 ,1 ), (2 ,2 )), 7 ((0 ,0 ), (0 ,1 ), (0 ,2 ), (0 ,3 )), 8 ((0 ,0 ), (0 ,1 ), (1 ,0 ), (1 ,1 )), 9) 10 11locked = set (shapes [0 ])| {(4 ,0 ), (5 ,0 )} 12translate = lambda p ,d :tuple ((x + d [0 ],y + d [1 ])for x ,y in p ) 13valid = lambda piece :all (p not in locked and p [0 ]in range (7 )for p in piece ) 14highest = 0 15count = 0 16cur = translate (shapes [0 ], (2 ,4 )) 17 18# def show(): 19# for y in range(1, highest + 5)[::-1]: 20# for x in range(7): 21# c = "." 22# if (x, y) in locked: 23# c = "#" 24# if (x, y) in cur: 25# c = "@" 26# print(c, end="") 27# print() 28# print() 29 30heights = [0 ] 31for d in cycle (open ("input" ).read ()[:- 1 ]): 32trans = translate (cur , ((d == ">" )* 2 - 1 ,0 )) 33if valid (trans ): 34cur = trans 35drop = translate (cur , (0 ,- 1 )) 36if valid (drop ): 37cur = drop 38else : 39for p in cur : 40locked .add (p ) 41count += 1 42highest = max (highest ,max (y for _ ,y in cur )) 43heights .append (highest ) 44if count == 2022 : 45highest ) 46if count == 10000 : 47break 48cur = translate (shapes [count % 5 ], (2 ,highest + 4 )) 49diffs = [heights [i + 1 ]- heights [i ]for i in range (len (heights )- 1 )] 50 51left ,right = 100 ,101 # in case the start isn't periodic cuz of the floor? 52while right < 10000 : 53if diffs [left ]== diffs [right ]: 54for i in range (5000 ): 55if diffs [left + i ]!= diffs [right + i ]: 56break 57else : 58"cycle detected with period" ,period := right - left ) 59break 60right += 1 61 62goal = 1_000_000_000_000 63cyclediff = sum (diffs [left :right ]) 64repeats = (goal - left )// period 65togo = goal - (left + repeats * (right - left )) 66heights [left ]+ cyclediff * repeats + sum (diffs [left :left + togo ]))