-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.py
156 lines (139 loc) · 5.38 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from math import ceil
def makeInput(file):
with open(file) as f:
lines = f.readlines()
inputLst = []
for x in lines:
line = x.strip().split('costs')
print(line)
i = 1
lst = []
while i < len(line):
current = line[i].split('and')
for y in current:
j = 0
while j < len(y) and y[j] == ' ':
j += 1
temp = y[j]
j += 1
while j < len(y) and y[j] != ' ':
temp += y[j]
j += 1
lst.append(int(temp))
i += 1
inputLst.append(lst)
dictLst = []
for lst in inputLst:
costDict = {}
i = 0
while i < 6:
if i ==0:
costDict['ore'] = {'ore':int(lst[i])} #costs ore
if i == 1:
costDict['clay'] = {'ore':int(lst[i])}#)costs ore
if i == 2:
costDict['obsidian'] = {'ore':int(lst[i]),'clay':int(lst[i+1])}#costs ore and clay
i += 1
if i == 4:
costDict['geode'] = {'ore':int(lst[i]), 'obsidian':int(lst[i+1])} #costs ore and obsidian
i += 1
i += 1
dictLst.append(costDict)
return dictLst
input = makeInput('day19test.txt')
# print(input)
# for costDict in input:
# for robot in costDict:
# print(costDict[robot])
resources = {'geode':0, 'obsidian':0, 'clay':0, 'ore':0}
robots = {'geode':0, 'obsidian':0, 'clay':0, 'ore':1,}
time = 24
def findMaxOutput(costDict, resources, robots, time):
# print(robots)
# print(time)
nextRobots = []
if robots['obsidian'] == 0:
if robots['clay'] == 0:
nextRobots.append('clay')
if robots['ore'] < maxResourceDict['ore']:
nextRobots.append('ore')
else:
nextRobots.append('obsidian')
if robots['clay'] < maxResourceDict['clay']:
nextRobots.append('clay')
if robots['ore'] < maxResourceDict['ore']:
nextRobots.append('ore')
else:
nextRobots.append('geode')
if robots['obsidian'] < maxResourceDict['obsidian']:
nextRobots.append('obsidian')
if robots['clay'] < maxResourceDict['clay']:
nextRobots.append('clay')
if robots['ore'] < maxResourceDict['ore']:
nextRobots.append('ore')
if nextRobots:
geodeLst = []
for nextRobot in nextRobots:
# resource + minutes * robots >= costDict[robot][resource]
# => minutes >= (costDict[robot][resource] - resources[resource]) / robots[robot]
maxMinsNeeded = 0
for resource in costDict[nextRobot]:
minsNeeded = ceil((costDict[nextRobot][resource] - resources[resource] )/ robots[resource])
maxMinsNeeded = max(maxMinsNeeded, minsNeeded)
maxMinsNeeded += 1
print(time, nextRobot, maxMinsNeeded)
if maxMinsNeeded < time:
tempResources = resources.copy()
tempRobots = robots.copy()
for resource in costDict[nextRobot]:
tempResources[resource] += (tempRobots[resource] * (maxMinsNeeded) - costDict[nextRobot][resource])
tempRobots[nextRobot] += 1
geodeLst.append(findMaxOutput(costDict,tempResources, tempRobots, time - maxMinsNeeded))
if geodeLst:
print(robots)
return max(geodeLst)
else:
print(robots)
return robots['geode'] * time
else:
print(robots)
return robots['geode'] * time
# for costDict in input:
costDict = input[0]
maxResourceDict = {}
for resource in resources:
maxResource = 0
for robot in costDict:
if resource in costDict[robot]:
maxResource = max(maxResource, costDict[robot][resource])
maxResourceDict[resource] = maxResource
print(maxResourceDict)
resources = {'geode':0, 'obsidian':0, 'clay':0, 'ore':0}
robots = {'geode':0, 'obsidian':0, 'clay':0, 'ore':1}
canBuildDict = {'geode':False, 'obsidian':False, 'clay':False, 'ore':False}
time = 24
print(findMaxOutput(costDict,resources,robots,time))
# if time > 0 :
# geodeLst = []
# for robot in canBuildDict:
# if canBuildDict[robot] == True:
# canBuildDict[robot] = False
# tempResources = resources.copy()
# tempRobots = robots.copy()
# tempCanBuildDict = canBuildDict.copy()
# otherTempResources = resources.copy()
# otherTempRobots = robots.copy()
# otherTempCanBuildDict = canBuildDict.copy()
# tempRobots[robot] += 1
# for resource in costDict[robot]:
# tempResources[resource] -= costDict[robot][resource]
# geodeLst.append(findMaxOutput(costDict, tempResources, tempRobots, time - 1, tempCanBuildDict))
# geodeLst.append(findMaxOutput(costDict, otherTempResources, otherTempRobots, time -1 , otherTempCanBuildDict))
# if geodeLst:
# return max(geodeLst)
# else:
# return findMaxOutput(costDict, resources, robots, time -1 , canBuildDict)
# else:
# print("robots=", robots)
# #print("resources=", resources)
# return resources['geode']