-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
197 lines (163 loc) · 5.49 KB
/
day14.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import re
data = """5 LKQCJ, 1 GDSDP, 2 HPXCL => 9 LVRSZ
5 HPXCL, 5 PVJGF => 3 KZRTJ
7 LVRSZ, 2 GFSZ => 5 FRWGJ
9 ZPTXL, 5 HGXJH, 9 LQMT => 7 LVCXN
2 LQMT, 2 PVJGF, 10 CKRVN => 9 VWJS
2 VRMXL, 12 NBRCS, 2 WSXN => 7 GDSDP
1 CKRP => 8 TBHVH
1 SVMNB, 2 KZRTJ => 8 WKGQS
6 LKQCJ, 8 HPXCL, 7 MPZH => 1 BQPG
1 RCWL => 7 MPZH
4 FGCMS, 2 LQMT, 1 LKQCJ => 1 KTBRM
1 ZTCSK, 6 CXQB, 2 ZBZRT => 3 PVJGF
7 DBNLM => 9 ZBZRT
5 BGNQ, 2 WBPD, 5 KTBRM => 9 GFSZ
6 XQBHG, 1 GPWVC => 8 CKFTS
1 XWLQM, 29 XQBHG, 7 KPNWG => 5 BXVL
6 TBHVH, 1 KTBRM => 7 HJGR
1 LQMT, 14 KPNWG => 7 GPWVC
18 LVCXN, 8 XVLT, 4 KPNWG, 13 LKQCJ, 12 M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CXQB => 5 VRMXL
1 SMFQ, 1 DWBW => 2 LHZMD""".splitlines()
# data = """171 ORE => 8 CNZTR
# 7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL
# 114 ORE => 4 BHXH
# 14 VRPVC => 6 BMBT
# 6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL
# 6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT
# 15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW
# 13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW
# 5 BMBT => 4 WPTQ
# 189 ORE => 9 KTJDG
# 1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP
# 12 VRPVC, 27 CNZTR => 2 XDBXC
# 15 KTJDG, 12 BHXH => 5 XCVML
# 3 BHXH, 2 VRPVC => 7 MZWV
# 121 ORE => 7 VRPVC
# 7 XCVML => 6 RJRHP
# 5 BHXH, 4 VRPVC => 5 LTCX""".splitlines()
from math import ceil
from collections import defaultdict
pattern = re.compile(r"(\d+) (\w+)")
recipes = [[[item for item in pattern.findall(line)]] for line in data]
for reaction in recipes:
for index, item in enumerate(reaction[0]):
reaction[0][index] = (int(item[0]), item[1])
reaction.append(reaction[0].pop())
leftover_ingredients = defaultdict(int) # type: ignore
float_leftover_ingredients = defaultdict(int) # type: ignore
def ingredients_for(quantity, product):
recipe = [i for i in recipes if i[1][1] == product][0]
ingredients, product_tuple = recipe
product_quantity = product_tuple[0]
recipe_times = ceil(quantity / product_quantity)
adjusted_ingredients = [
(i[0] * recipe_times - leftover_ingredients[i[1]], i[1]) for i in ingredients
]
final = []
for i, material in adjusted_ingredients:
leftover_ingredients[material] = 0
if i < 0:
leftover_ingredients[material] = -i
else:
final.append((i, material))
if product_quantity * recipe_times > quantity:
leftover_ingredients[product] += product_quantity * recipe_times - quantity
return final
def float_ingredients_for(quantity, product):
recipe = [i for i in recipes if i[1][1] == product][0]
ingredients, product_tuple = recipe
product_quantity = product_tuple[0]
recipe_times = quantity / product_quantity
adjusted_ingredients = [
(i[0] * recipe_times - float_leftover_ingredients[i[1]], i[1])
for i in ingredients
]
final = []
for i, material in adjusted_ingredients:
float_leftover_ingredients[material] = 0
if i < 0:
float_leftover_ingredients[material] = -i
else:
final.append((i, material))
if product_quantity * recipe_times > quantity:
float_leftover_ingredients[product] += (
product_quantity * recipe_times - quantity
)
return final
def calculate_ore(fuel_amount=1):
ore = 0
to_calculate = defaultdict(int)
to_calculate.update({"FUEL": fuel_amount})
while to_calculate:
material = list(to_calculate.keys())[0]
amount = to_calculate.pop(material)
ingredients = ingredients_for(amount, material)
for i in ingredients:
if i[1] == "ORE":
ore += i[0]
else:
to_calculate[i[1]] += i[0]
return ore
def calculate_ore_float(fuel_amount=1):
ore = 0
to_calculate = defaultdict(int)
to_calculate.update({"FUEL": fuel_amount})
while to_calculate:
material = list(to_calculate.keys())[0]
amount = to_calculate.pop(material)
ingredients = float_ingredients_for(amount, material)
for i in ingredients:
if i[1] == "ORE":
ore += i[0]
else:
to_calculate[i[1]] += i[0]
return ore
print(calculate_ore())
AVAILABLE_ORE = 1000000000000
max_fuel = int(AVAILABLE_ORE / ceil(calculate_ore_float()))
leftover_ingredients.clear()
while calculate_ore(max_fuel + 1) <= AVAILABLE_ORE:
max_fuel += 1
leftover_ingredients.clear()
print(int(max_fuel))