12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #!/usr/bin/python3
- import sys
- import re
- from itertools import product
- # part 1
- def simulate_factory(costs, plan, time=24):
- robots = [1, 0, 0, 0]
- resources = [0, 0, 0, 0]
- limit = time
- pi = 0
- builds = len(plan)
- while time > 0:
- # if anything is left to be built
- #if pi < builds:
- next_robots = robots.copy()
- if plan[pi] == '3' and resources[0] >= costs[3][0] and resources[2] >= costs[3][2]:
- resources[0] -= costs[3][0]
- resources[2] -= costs[3][2]
- next_robots[3] += 1
- pi += 1
- elif plan[pi] == '2' and resources[0] >= costs[2][0] and resources[1] >= costs[2][1]:
- resources[0] -= costs[2][0]
- resources[1] -= costs[2][1]
- next_robots[2] += 1
- pi += 1
- elif plan[pi] == '1' and resources[0] >= costs[1][0]:
- resources[0] -= costs[1][0]
- next_robots[1] += 1
- pi += 1
- elif plan[pi] == '0' and resources[0] >= costs[0][0]:
- resources[0] -= costs[0][0]
- next_robots[0] += 1
- pi += 1
- for i in range(len(robots)):
- resources[i] += robots[i]
- #print(next_robots, resources, limit - time + 1)
- robots = next_robots
- time -= 1
-
- geodes = resources[3]
- return geodes
- def part1():
- blueprints = []
- for line in sys.stdin:
- res = re.match(r'Blueprint ([0-9]+): Each ore robot costs ([0-9]+) ore. Each clay robot costs ([0-9]+) ore. Each obsidian robot costs ([0-9]+) ore and ([0-9]+) clay. Each geode robot costs ([0-9]+) ore and ([0-9]+) obsidian.', line.strip())
- if not res: continue
- blueprint = res.groups()
- costs = [int(b) for b in blueprint[1:]]
- cost_matrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
- cost_matrix[0][0] = costs[0]
- cost_matrix[1][0] = costs[1]
- cost_matrix[2][0] = costs[2]
- cost_matrix[2][1] = costs[3]
- cost_matrix[3][0] = costs[4]
- cost_matrix[3][2] = costs[5]
- blueprints.append((blueprint[0], cost_matrix))
- print(blueprints)
- qualities = []
- for blueprint in blueprints:
- print('simulating blueprint', blueprint)
- max_geodes = 0
- for plan in product('3210', repeat=24):
- if not ('0' in plan or '1' in plan or '2' in plan or '3' in plan): continue
- #print(plan)
- theoretical_max = 0
- for i, robot in enumerate(plan):
- if robot == '3':
- theoretical_max += (24 - i)
- if theoretical_max < max_geodes: continue
- geodes = simulate_factory(blueprint[1], plan)
- #if geodes > max_geodes: print(plan, geodes, max_geodes)
- max_geodes = max(max_geodes, geodes)
- qualities.append((int(blueprint[0]), max_geodes, int(blueprint[0]) * max_geodes))
- print(qualities)
- # part 2
- def part2():
- pass
- if sys.argv[1] in '1':
- part1()
- else:
- part2()
|