+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
a7718a0
master
1from operator import add ,sub 2 3lines = open ("input" ).readlines () 4lines = [[tuple (map (int ,x .split ("," )))for x in s .split ("->" )]for s in lines ] 5world = {} 6floor = max ((y for line in lines for _ ,y in line )) 7for line in lines : 8for i in range (1 ,len (line )): 9a ,b = line [i - 1 ],line [i ] 10diff = tuple (map (lambda a ,b : ((x := b - a ),x and x // abs (x )or 0 )[- 1 ],a ,b )) 11while a != b : 12world [a ]= "#" 13a = tuple (map (add ,a ,diff )) 14world [a ]= "#" 15 16# fmt: off 17def drop (has_floor ): 18x ,y = 500 ,0 19while True : 20if y > floor : 21if has_floor :world [(x ,y )]= "o" ;return (x ,y ) 22else :return False 23if not world .get ((x ,y + 1 )): 24y += 1 25elif not world .get ((x - 1 ,y + 1 )): 26x -= 1 ;y += 1 27elif not world .get ((x + 1 ,y + 1 )): 28x += 1 ;y += 1 29else : 30world [(x ,y )]= "o" ;return (x ,y ) 31# fmt: on 32 33 34orig = world .copy () 35while drop (False ): 36pass 37count = lambda :len ([a for a ,b in world .items ()if b == "o" ]) 38count ()) 39 40world = orig 41while last := drop (True )!= (500 ,0 ): 42pass 43count ())