adventofcode2022/day19/sol.py

136 lines
3.8 KiB
Python
Raw Normal View History

2022-12-19 07:08:55 +00:00
import re
sample = {
1:{
'ore': [4, 0, 0, 0],
'clay': [2, 0, 0, 0],
'obsidian': [3, 14, 0, 0],
'geode': [2, 0, 7, 0],
},
2:{
'ore': [2, 0, 0, 0],
'clay': [3, 0, 0, 0],
'obsidian': [3, 8, 0, 0],
'geode': [3, 0, 12, 0],
}}
robot_number = {'ore': 0, 'clay': 1, 'obsidian': 2, 'geode': 3, 'nothing': 99}
def parse(line):
line = re.sub(r'[^\d]+', ' ', line)
idx, ore1, ore2, ore3, clay3, ore4, obs4 = map(int, line.split())
return idx, {
'ore': [ore1, 0,0,0],
'clay': [ore2, 0,0,0],
'obsidian': [ore3,clay3,0,0],
'geode': [ore4,0,obs4,0],
}
def simulate(blueprint):
B = blueprint
resources = [0]*4
robots = [1,0,0,0]
2022-12-19 09:55:09 +00:00
rmax = [max(x[i] for x in B.values()) for i in range(3)] + [99]
2022-12-19 07:08:55 +00:00
worth = {}
worth['ore'] = 1
worth['clay'] = worth['ore']*B['clay'][0]
worth['obsidian'] = worth['ore']*B['obsidian'][0] + worth['clay']*B['obsidian'][1]
worth['geode'] = worth['ore']*B['geode'][0] + worth['obsidian']*B['geode'][2]
print(worth)
2022-12-19 09:55:09 +00:00
B_items = list(B.items())
B_items.reverse()
B_items.append(('nothing', [0,0,0,0]))
print(B_items)
2022-12-19 07:08:55 +00:00
minutes = 24
q = [(0, robots, resources)]
del resources
del robots
for _ in range(minutes):
2022-12-19 09:55:09 +00:00
next = [[] for _ in range(4)]
2022-12-19 07:08:55 +00:00
for _, robots, resources in q:
2022-12-19 09:55:09 +00:00
can_build = 0
for robot, cost in B_items:
2022-12-19 07:08:55 +00:00
i = robot_number[robot]
if robot != 'nothing' and robots[i] >= rmax[i]:
2022-12-19 09:55:09 +00:00
# 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
2022-12-19 07:08:55 +00:00
continue
if not all(x >= y for x, y in zip(resources, cost)):
2022-12-19 09:55:09 +00:00
# can't afford
2022-12-19 07:08:55 +00:00
continue
new = []
for x,y,z,m in zip(resources, robots, cost, rmax):
x = x+y-z
new.append(x)
if robot != 'nothing':
new_robots = list(robots)
new_robots[i] += 1
else:
new_robots = robots
2022-12-19 09:55:09 +00:00
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]))
#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)
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))
2022-12-19 07:08:55 +00:00
2022-12-19 09:55:09 +00:00
limit = 2500
2022-12-19 07:08:55 +00:00
2022-12-19 09:55:09 +00:00
q = []
for bucket in next:
bucket.sort(reverse=True)
q.extend(bucket[:limit])
2022-12-19 07:08:55 +00:00
2022-12-19 09:55:09 +00:00
count = sum(len(b) for b in next)
print(count, q[:1])
2022-12-19 07:08:55 +00:00
print(q[0])
2022-12-19 09:55:09 +00:00
return q[0][-1][-1]
2022-12-19 07:08:55 +00:00
#simulate(blueprints[1])
input = {}
with open('input') as f:
for line in f:
idx, bp = parse(line)
input[idx] = bp
def solve(input):
t = 0
for idx, B in input.items():
g = simulate(B)
t += idx*g
print(idx, g)
print("---")
print(t)
return t
assert solve(sample) == 33
solve(input)