Frans Bergman 2 жил өмнө
parent
commit
306b5d60e6
2 өөрчлөгдсөн 153 нэмэгдсэн , 0 устгасан
  1. 30 0
      19.input
  2. 123 0
      19.py

+ 30 - 0
19.input

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

+ 123 - 0
19.py

@@ -0,0 +1,123 @@
+from queue import PriorityQueue
+
+lines = [line.split(".") for line in open("19.input")]
+
+blueprints = [list(map(int,[line[0].split(" ")[6], line[1].split(" ")[5], line[2].split(" ")[5], line[2].split(" ")[8], line[3].split(" ")[5], line[3].split(" ")[8]])) for line in lines]
+
+
+def add(a, b):
+    return tuple(a_ + b_ for (a_, b_) in zip(list(a), list(b)))
+
+def sub(a, b):
+    return tuple(a_ - b_ for (a_, b_) in zip(list(a), list(b)))
+
+def lte(a, b):
+    return all(a_ <= b_ for (a_, b_) in zip(list(a), list(b)))
+
+
+def possible_purchases(res, robo, rem, costs, max_costs):
+    """Returns set of possible purchase tuples"""
+
+    result = set()
+
+    for robo_idx, cost in enumerate(costs):
+        if lte(cost, res):
+            result.add(robo_idx)
+
+    if 3 in result:
+        return {3}
+
+
+    for i in range(3):
+        if res[i] + robo[i] * rem >= max_costs[i] * rem:
+            result -= {i}
+
+    # For part 2, this can be changed to < instead of <= for more speed. ¯\_(ツ)_/¯
+    # I guess I was just lucky with my input...
+    if len(result) <=  2:
+        result.add(-1)
+
+    return result
+
+
+def find_max(bp_idx, blueprint, steps):
+    c_ore, c_clay, c_ob_ore, c_ob_clay, c_geo_ore, c_geo_ob = blueprint
+
+    q = PriorityQueue()
+
+    states = {}
+    results = {}
+    purc_cache = {}
+
+    res = (0, 0, 0, 0)
+    robo = (1, 0, 0, 0)
+
+    costs = [
+            (c_ore, 0, 0, 0),
+            (c_clay, 0, 0, 0),
+            (c_ob_ore, c_ob_clay, 0, 0),
+            (c_geo_ore, 0, c_geo_ob, 0),
+            ]
+
+    max_costs = [max(c[i] for c in costs) for i in range(4)]
+
+    start = (res, robo, steps)
+
+    q.put(start)
+
+    i = 0
+
+    while not q.empty():
+        current = q.get()
+        #print(len(states), current)
+        res, robo, rem = current
+        if res[0] > i:
+            i = res[0]
+            print(i)
+
+        if robo[0] >= c_geo_ore and robo[2] >= c_geo_ob:
+            print("Early exit")
+            states[current] = res[3] + robo[3] * rem + rem * rem // 2
+            continue
+
+        if rem == 0:
+            continue
+
+        for robo_idx in possible_purchases(res, robo, rem, costs, max_costs):
+            if robo_idx != -1:
+                new_purc = [0, 0, 0, 0]
+                new_purc[robo_idx] = 1
+                next_st = (add(sub(res, costs[robo_idx]), robo), add(robo, tuple(new_purc)), rem - 1)
+            else:
+                next_st = (add(res, robo), robo, rem - 1)
+
+            if next_st not in states:
+                states[next_st] = next_st[0][3]
+                q.put(next_st)
+
+    return max(s for s in states.values())
+
+
+def part1():
+    total = 0
+
+    for i, bp in enumerate(blueprints):
+        score = find_max(i, bp, 24)
+        print(i + 1, score)
+        total += (i + 1) * score
+
+    print("Part 1:", total)
+
+part1()
+
+def part2():
+    product = 1
+
+    for i, bp in enumerate(blueprints[:3]):
+        score = find_max(i, bp, 32)
+        print(i + 1, score)
+        product *= score
+
+    print("Part 2:", product)
+
+part2()