day 19 checkpoint
parent
fd84db7072
commit
98ff1e0cf9
63
day19/sol.py
63
day19/sol.py
|
@ -31,10 +31,8 @@ def simulate(blueprint):
|
||||||
resources = [0]*4
|
resources = [0]*4
|
||||||
robots = [1,0,0,0]
|
robots = [1,0,0,0]
|
||||||
|
|
||||||
rmax = [max(x[i] for x in B.values()) for i in range(3)]
|
rmax = [max(x[i] for x in B.values()) for i in range(3)] + [99]
|
||||||
rmax.append(99)
|
|
||||||
|
|
||||||
B['nothing'] = [0,0,0,0]
|
|
||||||
|
|
||||||
worth = {}
|
worth = {}
|
||||||
worth['ore'] = 1
|
worth['ore'] = 1
|
||||||
|
@ -45,19 +43,31 @@ def simulate(blueprint):
|
||||||
print(worth)
|
print(worth)
|
||||||
|
|
||||||
|
|
||||||
|
B_items = list(B.items())
|
||||||
|
B_items.reverse()
|
||||||
|
|
||||||
|
B_items.append(('nothing', [0,0,0,0]))
|
||||||
|
|
||||||
|
print(B_items)
|
||||||
|
|
||||||
minutes = 24
|
minutes = 24
|
||||||
q = [(0, robots, resources)]
|
q = [(0, robots, resources)]
|
||||||
del resources
|
del resources
|
||||||
del robots
|
del robots
|
||||||
for _ in range(minutes):
|
for _ in range(minutes):
|
||||||
next = []
|
next = [[] for _ in range(4)]
|
||||||
seen = set()
|
|
||||||
for _, robots, resources in q:
|
for _, robots, resources in q:
|
||||||
for robot, cost in B.items():
|
can_build = 0
|
||||||
|
for robot, cost in B_items:
|
||||||
i = robot_number[robot]
|
i = robot_number[robot]
|
||||||
if robot != 'nothing' and robots[i] >= rmax[i]:
|
if robot != 'nothing' and robots[i] >= rmax[i]:
|
||||||
|
# don't build more of 1 robot than we can spend in 1 minute
|
||||||
|
continue
|
||||||
|
if robot == 'nothing' and can_build == 3:
|
||||||
|
# always build something unless
|
||||||
continue
|
continue
|
||||||
if not all(x >= y for x, y in zip(resources, cost)):
|
if not all(x >= y for x, y in zip(resources, cost)):
|
||||||
|
# can't afford
|
||||||
continue
|
continue
|
||||||
|
|
||||||
new = []
|
new = []
|
||||||
|
@ -69,29 +79,38 @@ def simulate(blueprint):
|
||||||
new_robots[i] += 1
|
new_robots[i] += 1
|
||||||
else:
|
else:
|
||||||
new_robots = robots
|
new_robots = robots
|
||||||
key = str(new_robots)+str(new)
|
x = (new[3], max(new[3], new_robots[3]), max(new[2], new_robots[2]), max(new[1], new_robots[1]), max(new[0], new_robots[0]))
|
||||||
if key not in seen:
|
#w = sum(worth[r]*robots[robot_number[r]] for r in worth)
|
||||||
#x = (new[3], new_robots[3], new[2] + new[0], new_robots[2] + new_robots[0], new[1]+new[0], new_robots[1]+new_robots[0], new, new_robots)
|
#x = tuple(reversed(new+new_robots+[new[3]]))
|
||||||
w = sum(worth[r]*robots[robot_number[r]] for r in worth)
|
|
||||||
x = tuple(reversed(new+new_robots+[new[3]]))
|
|
||||||
#x = (new[3], new_robots)
|
#x = (new[3], new_robots)
|
||||||
next.append((x, new_robots, new))
|
n = sum(x==0 for x in new_robots)
|
||||||
|
if n == 3:
|
||||||
|
x = (new[0], new_robots[0])
|
||||||
|
elif n == 2:
|
||||||
|
x = (new[1], new[0], new_robots[1], new_robots[0])
|
||||||
|
elif n == 1:
|
||||||
|
x = (new_robots[3], min(new[2],new[0]), max(new[2],new[1]), new_robots[2], new_robots[1],new_robots[0])
|
||||||
|
else:
|
||||||
|
x = (new[3], min(new[2],new[0]), max(new[2],new[0]), new[1])
|
||||||
|
next[n].append((x,new_robots,new))
|
||||||
|
can_build += 1
|
||||||
|
if robot == 'geode':
|
||||||
|
# if we can build a geode then don't bother building anything else
|
||||||
|
break
|
||||||
|
#print(len(next))
|
||||||
|
|
||||||
bucket = {}
|
limit = 2500
|
||||||
for x,rob,res in next:
|
|
||||||
bucket.setdefault(x[0],[]).append((x,rob,res))
|
|
||||||
|
|
||||||
q = []
|
q = []
|
||||||
for i in bucket:
|
for bucket in next:
|
||||||
bucket[i].sort(reverse=True)
|
bucket.sort(reverse=True)
|
||||||
q.extend(bucket[i][:2000])
|
q.extend(bucket[:limit])
|
||||||
|
|
||||||
bucket.clear()
|
count = sum(len(b) for b in next)
|
||||||
|
print(count, q[:1])
|
||||||
print(len(q), q[:1])
|
|
||||||
|
|
||||||
print(q[0])
|
print(q[0])
|
||||||
return q[0][2][3]
|
return q[0][-1][-1]
|
||||||
|
|
||||||
#simulate(blueprints[1])
|
#simulate(blueprints[1])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue