summaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
Diffstat (limited to '2022')
-rwxr-xr-x2022/day19.py159
-rw-r--r--2022/in/day19.pzl30
-rw-r--r--2022/in/day19.ref2
3 files changed, 191 insertions, 0 deletions
diff --git a/2022/day19.py b/2022/day19.py
new file mode 100755
index 0000000..4d5da52
--- /dev/null
+++ b/2022/day19.py
@@ -0,0 +1,159 @@
+#!/usr/bin/env python3
+
+# import numpy as np
+from functools import reduce
+from re import findall
+from copy import deepcopy
+import sys
+
+# filename = "in/day19.ref"
+filename = "in/day19.pzl"
+data = open(filename).read()
+lines = [line for line in data.rstrip('\n').split('\n')]
+
+bps = list()
+
+for line in lines:
+ a = line.split(' ')
+ b = dict()
+ b['num'] = int(a[1].strip(':'))
+ b['ore_cost_ore'] = int(a[6])
+ b['cly_cost_ore'] = int(a[12])
+ b['obs_cost_ore'] = int(a[18])
+ b['obs_cost_cly'] = int(a[21])
+ b['geo_cost_ore'] = int(a[27])
+ b['geo_cost_obs'] = int(a[30])
+ bps.append(b)
+# print(bps)
+
+
+def solve(time, end_time, bp, s):
+ m = 0
+ needed_r_ore = max(bp['ore_cost_ore'],
+ bp['cly_cost_ore'],
+ bp['obs_cost_ore'],
+ bp['geo_cost_ore']
+ )
+ needed_r_cly = bp['obs_cost_cly']
+ needed_r_obs = bp['geo_cost_obs']
+
+ r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo = s
+
+ # if time == 10:
+ # print([len(i) for i in best_s])
+
+ if geo < best_geo[time]:
+ return 0
+ best_geo[time] = geo
+
+ if s in best_s[time]:
+ return 0
+ best_s[time].add(s)
+
+ if time == end_time:
+ return geo
+
+
+ # build none
+ r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo = s
+ if True:
+ ore += r_ore
+ cly += r_cly
+ obs += r_obs
+ geo += r_geo
+ sc = r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo
+ m = max(m, solve(time+1, end_time, bp, sc))
+
+ # build ore robot
+ r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo = s
+ if ore >= bp['ore_cost_ore'] and r_ore < needed_r_ore:
+ ore -= bp['ore_cost_ore']
+
+ ore += r_ore
+ cly += r_cly
+ obs += r_obs
+ geo += r_geo
+ r_ore += 1
+
+ sc = r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo
+ m = max(m, solve(time+1, end_time, bp, sc))
+
+ # build cly robot
+ r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo = s
+ if ore >= bp['cly_cost_ore'] and r_cly < needed_r_cly:
+ ore -= bp['cly_cost_ore']
+
+ ore += r_ore
+ cly += r_cly
+ obs += r_obs
+ geo += r_geo
+ r_cly += 1
+
+ sc = r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo
+ m = max(m, solve(time+1, end_time, bp, sc))
+
+ # build obs robot
+ r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo = s
+ if ore >= bp['obs_cost_ore'] and cly >= bp['obs_cost_cly'] and r_obs < needed_r_obs:
+ ore -= bp['obs_cost_ore']
+ cly -= bp['obs_cost_cly']
+
+ ore += r_ore
+ cly += r_cly
+ obs += r_obs
+ geo += r_geo
+ r_obs += 1
+
+ sc = r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo
+ m = max(m, solve(time+1, end_time, bp, sc))
+
+ # build geo robot
+ r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo = s
+ if ore >= bp['geo_cost_ore'] and obs >= bp['geo_cost_obs'] :
+ ore -= bp['geo_cost_ore']
+ obs -= bp['geo_cost_obs']
+
+ ore += r_ore
+ cly += r_cly
+ obs += r_obs
+ geo += r_geo
+ r_geo += 1
+
+ sc = r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo
+ m = max(m, solve(time+1, end_time, bp, sc))
+
+ return m
+
+
+r_ore = 1
+r_cly = 0
+r_obs = 0
+r_geo = 0
+ore = 0
+cly = 0
+obs = 0
+geo = 0
+s = r_ore, r_cly, r_obs, r_geo, ore, cly, obs, geo
+
+
+res1 = 0
+end_time = 24
+for num,bp in enumerate(bps):
+ best_s = [set() for i in range(end_time + 1)]
+ best_geo = [0 for i in range(end_time + 1)]
+ res = solve(0, end_time, bp, s)
+ # print(res)
+ res1 += (num+1) * res
+
+res2 = 1
+end_time = 32
+for bp in bps[:3]:
+ best_s = [set() for i in range(end_time + 1)]
+ best_geo = [0 for i in range(end_time + 1)]
+ res = solve(0, end_time, bp, s)
+ # print(res)
+ res2 *= res
+
+print('res1:', res1)
+print('res2:', res2)
+
diff --git a/2022/in/day19.pzl b/2022/in/day19.pzl
new file mode 100644
index 0000000..2962e01
--- /dev/null
+++ b/2022/in/day19.pzl
@@ -0,0 +1,30 @@
+Blueprint 1: 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 3 ore and 8 obsidian.
+Blueprint 2: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 18 clay. Each geode robot costs 4 ore and 20 obsidian.
+Blueprint 3: 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 4: 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 8 obsidian.
+Blueprint 5: 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.
+Blueprint 6: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 17 clay. Each geode robot costs 4 ore and 8 obsidian.
+Blueprint 7: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 6 clay. Each geode robot costs 3 ore and 11 obsidian.
+Blueprint 8: 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 9: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 16 clay. Each geode robot costs 3 ore and 15 obsidian.
+Blueprint 10: 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 11 obsidian.
+Blueprint 11: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 9 clay. Each geode robot costs 3 ore and 19 obsidian.
+Blueprint 12: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 17 clay. Each geode robot costs 3 ore and 16 obsidian.
+Blueprint 13: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 5 clay. Each geode robot costs 3 ore and 12 obsidian.
+Blueprint 14: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 2 ore and 12 obsidian.
+Blueprint 15: 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 3 ore and 20 obsidian.
+Blueprint 16: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 11 clay. Each geode robot costs 2 ore and 19 obsidian.
+Blueprint 17: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 10 clay. Each geode robot costs 3 ore and 14 obsidian.
+Blueprint 18: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 12 clay. Each geode robot costs 3 ore and 17 obsidian.
+Blueprint 19: 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 19 obsidian.
+Blueprint 20: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 17 clay. Each geode robot costs 3 ore and 19 obsidian.
+Blueprint 21: 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 22: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 4 ore and 15 clay. Each geode robot costs 4 ore and 9 obsidian.
+Blueprint 23: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 9 clay. Each geode robot costs 3 ore and 7 obsidian.
+Blueprint 24: 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 17 obsidian.
+Blueprint 25: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 9 clay. Each geode robot costs 2 ore and 20 obsidian.
+Blueprint 26: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 20 clay. Each geode robot costs 3 ore and 14 obsidian.
+Blueprint 27: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 15 clay. Each geode robot costs 3 ore and 16 obsidian.
+Blueprint 28: 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 29: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 3 ore and 14 obsidian.
+Blueprint 30: 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 3 ore and 13 obsidian.
diff --git a/2022/in/day19.ref b/2022/in/day19.ref
new file mode 100644
index 0000000..f39c094
--- /dev/null
+++ b/2022/in/day19.ref
@@ -0,0 +1,2 @@
+Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian.
+Blueprint 2: 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 12 obsidian.