-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.py
93 lines (76 loc) · 3.59 KB
/
day19.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
import numpy as np
from collections import defaultdict
from heapq import heapify, heappop, heappush
def optimalSolution(seenStates, startingRessources, startingRobots, robotCosts, time, currentMax):
if time == 0:
return startingRessources[3]
state = str([time]+list(startingRobots)+list(startingRessources))
maxGeodes = seenStates[state]
if maxGeodes > 0:
return maxGeodes
maxGeodes = startingRessources[3] + startingRobots[3]*time
upperGeodesEstimation = ((time + startingRobots[3]) * (time + startingRobots[3] + 1)) // 2 - (startingRobots[3] * (startingRobots[3] + 1)) // 2 + startingRessources[3]
if upperGeodesEstimation <= currentMax:
return 0
for i in reversed(range(4)):
if i < 3 and np.all(startingRobots[i] >= robotCosts[:,i]):
continue
timeBeforeBuildingRobot = -1
for j in range(4):
if startingRobots[j] == 0 and robotCosts[i,j] > 0:
timeBeforeBuildingRobot = 1000
elif startingRobots[j] == 0:
timeBeforeBuildingRobot = max(-1, timeBeforeBuildingRobot)
elif startingRessources[j] >= robotCosts[i,j]:
timeBeforeBuildingRobot = max(0, timeBeforeBuildingRobot)
else:
timeBeforeBuildingRobot = max(timeBeforeBuildingRobot, np.ceil((robotCosts[i,j] - startingRessources[j]) / startingRobots[j]))
if timeBeforeBuildingRobot < 0:
continue
elif timeBeforeBuildingRobot < time:
newTime = time - (timeBeforeBuildingRobot+1)
newRobots = startingRobots.copy()
newRobots[i] += 1
newRessources = startingRessources + startingRobots*(timeBeforeBuildingRobot+1) - robotCosts[i, :]
maxGeodes = max(maxGeodes, optimalSolution(seenStates, newRessources, newRobots, robotCosts, newTime, maxGeodes))
if i == 4:
seenStates[state] = maxGeodes
return maxGeodes
seenStates[state] = maxGeodes
return maxGeodes
f = open("ressources/day19.txt", "r")
lines = f.readlines()
ressourcesLabel = ["ore", "clay", "obsidian", "geode"]
total = 0
for line in lines:
blueprint = line.replace("\n", "").replace(":", "").replace(".", "").split()
startingRessources = np.array([0, 0, 0, 0])
startingRobots = np.array([1, 0, 0, 0])
robotCosts = np.zeros((4,4))
id = blueprint[1]
robotCosts[0, 0] = blueprint[6]
robotCosts[1, 0] = blueprint[12]
robotCosts[2, 0], robotCosts[2, 1] = blueprint[18], blueprint[21]
robotCosts[3, 0], robotCosts[3, 2] = blueprint[27], blueprint[30]
seenStates = defaultdict(int)
maxGeodes = optimalSolution(seenStates, startingRessources, startingRobots, robotCosts, 24, 0)
print(id, maxGeodes)
total += int(id) * maxGeodes
print("Quality level of blueprints is: {}".format(total))
total = 1
for line in lines[0:3]:
blueprint = line.replace("\n", "").replace(":", "").replace(".", "").split()
startingRessources = np.array([0, 0, 0, 0])
startingRobots = np.array([1, 0, 0, 0])
robotCosts = np.zeros((4,4))
id = blueprint[1]
robotCosts[0, 0] = blueprint[6]
robotCosts[1, 0] = blueprint[12]
robotCosts[2, 0], robotCosts[2, 1] = blueprint[18], blueprint[21]
robotCosts[3, 0], robotCosts[3, 2] = blueprint[27], blueprint[30]
seenStates = defaultdict(int)
maxGeodes = optimalSolution(seenStates, startingRessources, startingRobots, robotCosts, 32, 0)
print(id, maxGeodes)
total *= maxGeodes
print("Quality level of blueprints is: {}".format(total))