+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
0579d0a
master
1from operator import ge ,sub 2 3ORE ,CLAY ,OBS ,GEO = range (4 ) 4build = lambda t ,r :t [:r ]+ (t [r ]+ 1 ,)+ t [r + 1 :] 5take = lambda a ,b :tuple (map (sub ,a ,b )) 6better = enough = lambda a ,b :all (map (ge ,a ,b )) 7worse = lambda a ,b :all (map (lt ,a ,b )) 8 9 10# naive tree search is too slow, have to memoize or eary cutoff somehow 11# is it ever suboptimal to build a geo bot when you can? 12def rec (prices ,bots ,mats ,score ,t ): 13# print(t, bots, mats) 14new_mats = tuple (a + b for a ,b in zip (bots ,mats )) 15if not t : 16return score 17best = score 18if enough (mats ,prices [GEO ]): 19return rec (prices ,bots ,take (new_mats ,prices [GEO ]),score + t - 1 ,t - 1 ) 20for i in range (3 ): 21if enough (mats ,prices [i ]): 22tmp = rec (prices ,build (bots ,i ),take (new_mats ,prices [i ]),score ,t - 1 ) 23best = max (best ,tmp ) 24best = max (best ,rec (prices ,bots ,new_mats ,score ,t - 1 )) 25return best 26 27 28def best (blueprint ): 29ore ,clay ,obs_ore ,obs_clay ,geo_ore ,geo_obs = blueprint 30prices = ( 31 (ore ,0 ,0 ), 32 (clay ,0 ,0 ), 33 (obs_ore ,obs_clay ,0 ), 34 (geo_ore ,0 ,geo_obs ), 35 ) 36bots = (1 ,0 ,0 ) 37mats = (0 ,0 ,0 ) 38return rec (prices ,bots ,mats ,0 ,24 ) 39 40 41blueprints = [tuple (int (x )for x in s .split ()if x .isnumeric ())for s in open ("small" )] 42best (blueprints [0 ])) 43# print(sum(map(best, blueprints)))