SHARE
TWEET

AoC 2019 Day 14 Part 2

jangxx Dec 14th, 2019 145 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from gurobipy import *
  2. import re
  3.  
  4. ruleRe = re.compile("(\d+) (\w+)")
  5.  
  6. with open("input14.txt") as f:
  7.     model = Model("fuel")
  8.  
  9.     chemicals = {}
  10.     ruleVars = {}
  11.     chemicals2rules = {}
  12.     leftovers = {}
  13.  
  14.     rules = []
  15.  
  16.     model.modelSense = GRB.MAXIMIZE
  17.  
  18.     for i,line in enumerate(f):
  19.         rule = line.split("=>")
  20.  
  21.         lh = [ ruleRe.match(r.strip()).groups() for r in rule[0].strip().split(",") ]
  22.         rh = ruleRe.match(rule[1].strip()).groups()
  23.  
  24.         # add vars
  25.         for quant,chem in lh:
  26.             if not chem in chemicals:
  27.                 chemicals2rules[chem] = { "lh": [], "rh": [] }
  28.                 chemicals[chem] = model.addVar(vtype=GRB.INTEGER, lb=0, obj=0)
  29.  
  30.             chemicals2rules[chem]["lh"].append((i, int(quant)))
  31.  
  32.         if not rh[1] in chemicals:
  33.             chemicals2rules[rh[1]] = { "lh": [], "rh": [] }
  34.             chemicals[rh[1]] = model.addVar(vtype=GRB.INTEGER, lb=0, obj=0)
  35.  
  36.         chemicals2rules[rh[1]]["rh"].append((i, int(rh[0])))
  37.  
  38.         ruleVars[i] = model.addVar(vtype=GRB.INTEGER, lb=0, obj=0)
  39.    
  40.         rules.append((lh, rh))
  41.  
  42.     chemicals["FUEL"].obj = 1
  43.     # chemicals["ORE"].obj = 0
  44.  
  45.     for chem in chemicals:
  46.         leftovers[chem] = model.addVar(vtype=GRB.INTEGER, lb=0, obj=-1)
  47.  
  48.     # add rules
  49.     for chem in chemicals:
  50.         if len(chemicals2rules[chem]["rh"]) > 0:
  51.             # the quantity of chemicals is equal to the produced amount
  52.             model.addConstr(quicksum(quant * ruleVars[rule] for rule,quant in chemicals2rules[chem]["rh"]) == chemicals[chem])
  53.  
  54.         if len(chemicals2rules[chem]["lh"]) > 0:
  55.             # the quantity of chemicals has to be equal or larger than the needed amount for all rules
  56.             model.addConstr(quicksum(quant * ruleVars[rule] for rule,quant in chemicals2rules[chem]["lh"]) <= chemicals[chem])
  57.  
  58.             # the leftovers are >produced amount< - >required amount<
  59.             model.addConstr(chemicals[chem] - quicksum(quant * ruleVars[rule] for rule,quant in chemicals2rules[chem]["lh"]) == leftovers[chem])
  60.  
  61.     model.addConstr(chemicals["ORE"] == 1000000000000)
  62.  
  63.     model.optimize()
  64.  
  65.     # print solution
  66.     for chem in chemicals:
  67.         print("Require {} of type {}".format(chemicals[chem].x, chem))
  68.  
  69.     for r in ruleVars:
  70.         print("Rule {} was applied {} times".format(r, ruleVars[r].x))
  71.  
  72.     print(chemicals["FUEL"].x)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top