-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
113 lines (88 loc) · 2.42 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
from collections import deque
import re
def sim(ore_cost, clay_cost, obs_cost, geo_cost, time=24, p2=False):
q = deque([(0, 1, 0, 0, 0, 0, 0, 0, time)])
visited = set()
best = 0
max_ore_needed = max(ore_cost, clay_cost, obs_cost[0], geo_cost[0])
while q:
ore, rore, clay, rclay, obs, robs, geo, rgeo, time = x = q.pop()
if x in visited:
continue
visited.add(x)
rore = min(rore, max_ore_needed)
new_ore = ore + rore
new_clay = clay + rclay
new_obs = obs + robs
new_geo = geo + rgeo
time -= 1
if time == 0:
best = max(best, new_geo)
continue
build = False
if ore >= geo_cost[0] and obs >= geo_cost[1]:
new_ore2 = new_ore - geo_cost[0]
new_obs2 = new_obs - geo_cost[1]
new_rgeo = rgeo + 1
q.append((new_ore2, rore, new_clay, rclay,
new_obs2, robs, new_geo, new_rgeo, time))
build = True
# Probably wrong, but works anyway...
continue
if ore >= obs_cost[0] and clay >= obs_cost[1]:
new_ore2 = new_ore - obs_cost[0]
new_clay2 = new_clay - obs_cost[1]
new_robs = robs + 1
q.append((new_ore2, rore, new_clay2, rclay,
new_obs, new_robs, new_geo, rgeo, time))
build = True
if ore >= clay_cost:
new_ore2 = new_ore - clay_cost
new_rclay = rclay + 1
q.append((new_ore2, rore, new_clay, new_rclay,
new_obs, robs, new_geo, rgeo, time))
build = True
if ore >= ore_cost:
new_ore2 = new_ore - ore_cost
new_rore = rore + 1
q.append((new_ore2, new_rore, new_clay, rclay,
new_obs, robs, new_geo, rgeo, time))
build = True
# Technically wrong, but magically works for the first 3 blueprints :')
# if not build:
if not p2 or not build:
q.append((new_ore, rore, new_clay, rclay, new_obs, robs, new_geo, rgeo, time))
return best
lines = open("input/input.txt").readlines()
ans = 0
r = re.compile(r'\d+')
bp = []
for l in lines:
# l = l.split()
nums = list(map(int, r.findall(l)))
bid = nums[0]
ore_cost = nums[1]
clay_cost = nums[2]
obs_cost = (nums[3], nums[4])
geo_cost = (nums[5], nums[6])
bp.append((bid, ore_cost, clay_cost, obs_cost, geo_cost))
# eprint(bp[-1])
# timer_start('x')
tot = 0
for bid, *info in bp:
s = sim(*info)
# timer_lap('x')
# eprint(bid, s)
tot += bid * s
# timer_stop('x')
print(1, tot)
# timer_start('x')
tot = 1
for bid, *info in bp[:3]:
s = sim(*info, time=32, p2=True)
# timer_lap('x')
# eprint(bid, s)
tot *= s
# timer_stop('x')
print(2, tot)