Taddeus Kroes 2 жил өмнө
parent
commit
d2a086f9bf
2 өөрчлөгдсөн 84 нэмэгдсэн , 0 устгасан
  1. 54 0
      2022/19_robots.py
  2. 30 0
      2022/input/19

+ 54 - 0
2022/19_robots.py

@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+import sys
+import re
+from math import ceil
+
+def parse_blueprint(line):
+    ore_ore, clay_ore, obs_ore, obs_clay, geo_ore, geo_obs = \
+            map(int, re.findall(r'\d+', line)[1:])
+    return (
+        (0, geo_obs, 0, geo_ore),
+        (0, 0, obs_clay, obs_ore),
+        (0, 0, 0, clay_ore),
+        (0, 0, 0, ore_ore),
+    )
+
+def max_geo(blueprint, time):
+    work = [(time, (0, 0, 0, 0), (0, 0, 0, 1))]
+    best = 0
+
+    # We can only make 1 robot per turn so we should only produce the maximum of
+    # any single material cost per turn.
+    max_robots = [time] + [max(c[ty] for c in blueprint) for ty in range(1, 4)]
+
+    while work:
+        time, materials, robots = state = work.pop()
+        final_geo = materials[0] + time * robots[0]
+
+        # Best case we'll keep building a geo robot each turn from now on.
+        # If even that is not enough to improve, ignore the state.
+        if final_geo + sum(range(1, time)) < best:
+            continue
+
+        # Accumulate geodes in the remaining time, and try to improve by
+        # building more robots.
+        best = max(best, final_geo)
+
+        for ty, cost in enumerate(blueprint):
+            # Skip time until we have accumulated the material cost, then wait 1
+            # turn for production. A very low default denominator approximates
+            # infinite build time for robots requiring unproduced materials.
+            build_time = 1 + max(ceil(max(c - m, 0) / (r or 0.0001))
+                                 for c, m, r in zip(cost, materials, robots))
+            if build_time < time and robots[ty] < max_robots[ty]:
+                newmat = tuple(m + r * build_time - c
+                               for c, m, r in zip(cost, materials, robots))
+                newrobots = tuple(r + (i == ty) for i, r in enumerate(robots))
+                work.append((time - build_time, newmat, newrobots))
+
+    return best
+
+blueprints = list(map(parse_blueprint, sys.stdin))
+print(sum((i + 1) * max_geo(bp, 24) for i, bp in enumerate(blueprints)))
+a, b, c = (max_geo(bp, 32) for bp in blueprints[:3])
+print(a * b * c)

+ 30 - 0
2022/input/19

@@ -0,0 +1,30 @@
+Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 14 clay. Each geode robot costs 2 ore and 16 obsidian.
+Blueprint 2: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 19 clay. Each geode robot costs 3 ore and 17 obsidian.
+Blueprint 3: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 19 clay. Each geode robot costs 3 ore and 10 obsidian.
+Blueprint 4: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 12 clay. Each geode robot costs 2 ore and 10 obsidian.
+Blueprint 5: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 11 clay. Each geode robot costs 2 ore and 7 obsidian.
+Blueprint 6: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 17 clay. Each geode robot costs 3 ore and 8 obsidian.
+Blueprint 7: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 4 ore and 19 obsidian.
+Blueprint 8: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 7 clay. Each geode robot costs 4 ore and 20 obsidian.
+Blueprint 9: Each ore robot costs 2 ore. Each clay robot costs 2 ore. Each obsidian robot costs 2 ore and 10 clay. Each geode robot costs 2 ore and 11 obsidian.
+Blueprint 10: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 4 ore and 17 obsidian.
+Blueprint 11: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 3 ore and 8 obsidian.
+Blueprint 12: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 16 clay. Each geode robot costs 2 ore and 15 obsidian.
+Blueprint 13: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 6 clay. Each geode robot costs 3 ore and 16 obsidian.
+Blueprint 14: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 5 clay. Each geode robot costs 4 ore and 11 obsidian.
+Blueprint 15: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 20 clay. Each geode robot costs 3 ore and 18 obsidian.
+Blueprint 16: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 17 clay. Each geode robot costs 4 ore and 20 obsidian.
+Blueprint 17: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 7 clay. Each geode robot costs 2 ore and 16 obsidian.
+Blueprint 18: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 4 ore and 12 obsidian.
+Blueprint 19: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 5 clay. Each geode robot costs 2 ore and 10 obsidian.
+Blueprint 20: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 4 ore and 9 obsidian.
+Blueprint 21: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 16 clay. Each geode robot costs 3 ore and 20 obsidian.
+Blueprint 22: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian.
+Blueprint 23: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 20 clay. Each geode robot costs 2 ore and 19 obsidian.
+Blueprint 24: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 20 clay. Each geode robot costs 4 ore and 18 obsidian.
+Blueprint 25: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 10 clay. Each geode robot costs 4 ore and 8 obsidian.
+Blueprint 26: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 8 clay. Each geode robot costs 3 ore and 9 obsidian.
+Blueprint 27: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 15 clay. Each geode robot costs 2 ore and 13 obsidian.
+Blueprint 28: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 15 clay. Each geode robot costs 4 ore and 20 obsidian.
+Blueprint 29: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 6 clay. Each geode robot costs 4 ore and 11 obsidian.
+Blueprint 30: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 20 clay. Each geode robot costs 2 ore and 20 obsidian.