Advertisement
Guest User

Untitled

a guest
Dec 19th, 2022
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | Source Code | 0 0
  1. import re
  2. from collections import namedtuple
  3.  
  4. STEPS = 32
  5.  
  6. from multiprocessing import Process, Queue
  7. def qual(env, ore_rbt, clay_rbt, obs_rbt, geo_rbt, ore_cnt, clay_cnt, obs_cnt, geo_cnt, t):
  8.     assert max(ore_rbt, clay_rbt, obs_rbt, geo_rbt) < 32
  9.     assert max(ore_cnt, clay_cnt, obs_cnt) < 512
  10.     assert geo_cnt < 128
  11.     code = (t | ore_rbt <<6 | (clay_rbt <<11)| (obs_rbt <<16) | (geo_rbt << 21) |
  12.             (ore_cnt <<26) | (clay_cnt <<35) | (obs_cnt <<44) | (geo_cnt << 53))
  13.     if code in env.cache:
  14.         return env.cache[code]
  15.     if t == STEPS:
  16.         return geo_cnt
  17.  
  18.     # if fewer geodes than 80% of best solution so far -> discard
  19.     if geo_cnt < env.t_max[t]*8//10:
  20.         return -1
  21.  
  22.     env.t_max[t] = max(env.t_max[t], geo_cnt)
  23.  
  24.     n_ore_cnt = ore_cnt + ore_rbt
  25.     n_clay_cnt = clay_cnt + clay_rbt
  26.     n_obs_cnt = obs_cnt + obs_rbt
  27.     n_geo_cnt = geo_cnt + geo_rbt
  28.  
  29.     maxg = 0
  30.     if ore_cnt >= env.ore_cost and ore_rbt < env.max_ore_needed:
  31.         maxg = max(maxg, qual(env, ore_rbt+1, clay_rbt, obs_rbt, geo_rbt, n_ore_cnt - env.ore_cost, n_clay_cnt, n_obs_cnt, n_geo_cnt, t+1))
  32.  
  33.     if ore_cnt >= env.clay_cost and clay_rbt < env.obs_cost_clay:
  34.         maxg = max(maxg, qual(env, ore_rbt, clay_rbt+1, obs_rbt, geo_rbt, n_ore_cnt - env.clay_cost, n_clay_cnt, n_obs_cnt, n_geo_cnt, t+1))
  35.  
  36.     if ore_cnt >= env.obs_cost_ore and clay_cnt >= env.obs_cost_clay and obs_rbt < env.geo_cost_obs:
  37.         maxg = max(maxg, qual(env, ore_rbt, clay_rbt, obs_rbt+1, geo_rbt, n_ore_cnt - env.obs_cost_ore, n_clay_cnt-env.obs_cost_clay, n_obs_cnt, n_geo_cnt, t+1))
  38.  
  39.     if ore_cnt >= env.geo_cost_ore and obs_cnt >= env.geo_cost_obs:
  40.         maxg = max(maxg, qual(env, ore_rbt, clay_rbt, obs_rbt, geo_rbt+1, n_ore_cnt - env.geo_cost_ore, n_clay_cnt, n_obs_cnt-env.geo_cost_obs, n_geo_cnt, t+1))
  41.  
  42.     maxg = max(maxg, qual(env, ore_rbt, clay_rbt, obs_rbt, geo_rbt, n_ore_cnt, n_clay_cnt, n_obs_cnt, n_geo_cnt, t+1))
  43.  
  44.     env.cache[code] = maxg
  45.     return maxg
  46.  
  47. Env = namedtuple("Env", "ore_cost, clay_cost, obs_cost_ore, obs_cost_clay, geo_cost_ore, geo_cost_obs, cache, t_max, max_ore_needed")
  48.  
  49. def runline(queue, line):
  50.     bp, ore_cost, clay_cost, obs_cost_ore, obs_cost_clay, geo_cost_ore, geo_cost_obs = [int(x) for x in re.findall("\d+", line)]
  51.     cache={}
  52.     t_max={i:0 for i in range(STEPS)}
  53.     max_ore_needed = max(ore_cost, clay_cost, obs_cost_ore, geo_cost_ore)
  54.     env = Env(ore_cost, clay_cost, obs_cost_ore, obs_cost_clay, geo_cost_ore, geo_cost_obs, cache, t_max, max_ore_needed)
  55.     q = qual(env, 1, 0, 0, 0, 0, 0, 0, 0, 0)
  56.     print (bp, "->", q)
  57.     queue.put(q)
  58.  
  59. with open("geode_p2.txt") as infile:
  60.     procs = []
  61.  
  62.     for line in infile:
  63.         queue = Queue()
  64.         proc = Process(target=runline, args=(queue, line))
  65.         proc.start()
  66.         procs.append((proc, queue))
  67.  
  68.     total = 1
  69.     for p, q in procs:
  70.         p.join() # this blocks until the process terminates
  71.         result = q.get()
  72.         print (result)
  73.         total *= result
  74.     print (total)
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement