Advertisement
Guest User

Untitled

a guest
Dec 19th, 2022
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. import numpy as np
  2. from cpmpy import *
  3. np.set_printoptions(edgeitems=30, linewidth=100000)
  4.  
  5. # Read input
  6. fileObj = open('input', "r")
  7. words = fileObj.read().splitlines()
  8. fileObj.close()
  9. blueprints = []
  10. for w in words:
  11.     spl = w.split(' ')
  12.     blueprints.append(list(map(int, [spl[6], spl[12], spl[18], spl[21], spl[27], spl[30]])))
  13.  
  14. ORE = 0
  15. CLAY = 1
  16. OBSIDIAN = 2
  17. GEODE = 3
  18.  
  19. NR_OF_STEPS = 32  # 24 for part 1
  20.  
  21.  
  22. qualityLevels = []
  23. for index in range(3):  # range(len(blueprints) for part 1
  24.     bp = blueprints[index]
  25.     a, b, c, d, e, f = bp
  26.     costArray = cpm_array([
  27.         [0, 0, 0, 0],
  28.         [a, 0, 0, 0],
  29.         [b, 0, 0, 0],
  30.         [c, d, 0, 0],
  31.         [e, 0, f, 0],
  32.     ])
  33.     # Lengths are NR_OF_STEPS+1 so we can have starting values at Minute 0
  34.     botsPerStep = intvar(0, 9999, shape=(4, NR_OF_STEPS + 1), name="bots")  # Array of amount of bots per step per type
  35.     resourcesPerStep = intvar(0, 9999, shape=(4, NR_OF_STEPS + 1), name="resources")  # Array of amount of resources per step per type
  36.  
  37.     building = intvar(-1, 3, shape=(1, NR_OF_STEPS+1), name="building")  # -1 = not building anything, bigger x means "building a robot that can harvest resource x"
  38.     blueprintModel = Model(
  39.     )
  40.     for r in range(4):  # For every resource type...
  41.         blueprintModel += (botsPerStep[r, 0] == (1 if r == ORE else 0))  # Start with 1 ore robot
  42.         blueprintModel += (resourcesPerStep[r, 0] == 0)  # Start with 0 resources
  43.  
  44.         for s in range(1, NR_OF_STEPS+1):  # For every step...
  45.             blueprintModel += (resourcesPerStep[r, s] == resourcesPerStep[r, s-1] >= costArray[building[0, s] + 1, r])  # Don't spend resources before you have them
  46.             blueprintModel += (resourcesPerStep[r, s] == resourcesPerStep[r, s-1] + botsPerStep[r, s-1] - costArray[building[0, s]+1, r])  # Bots of type r make 1 resource per step, subtract resources you spent on robots
  47.             blueprintModel += (botsPerStep[r, s] == (botsPerStep[r, s-1]) + (building[0, s] == r))  # Keep your robots, + 1 if you built one
  48.  
  49.     blueprintModel.maximize(resourcesPerStep[3][NR_OF_STEPS])  # Objective: maximize the number of geodes in the end
  50.     if blueprintModel.solve():
  51.         print("solved blueprint %s with max geodes %s" % ((index+1), resourcesPerStep[3][NR_OF_STEPS].value()))
  52.         # print(costArray)
  53.         # print(bots, resources and building per step)
  54.         # print(np.row_stack((botsPerStep.value(),resourcesPerStep.value(), building.value())))
  55.         qualityLevels.append((index+1) * resourcesPerStep[3][NR_OF_STEPS].value())
  56.     else:
  57.         print("No solution found")
  58. print(qualityLevels)
  59. print(sum(qualityLevels))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement