Skip to content

Commit 873c766

Browse files
committed
Day 14 part 2
1 parent 5f08e33 commit 873c766

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

day14/d14.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,35 @@
2020
continue
2121
distance[material] = max([distance[i] for i in recipes[material]['ingredients'].keys()]) + 1
2222

23-
needed = {'FUEL': 1}
24-
while len(needed) > 1 or 'ORE' not in needed:
25-
material = max(needed, key=lambda x: distance[x])
26-
quantity = needed[material]
27-
del needed[material]
28-
if material == 'ORE':
29-
needed[material] = quantity
30-
continue
31-
base_quantity, ingredients = recipes[material].values()
32-
for a, b in ingredients.items():
33-
if a not in needed:
34-
needed[a] = 0
35-
needed[a] += math.ceil(quantity/base_quantity)*b
3623

37-
print('Part 1:', needed['ORE'])
24+
def required_ore(fuel):
25+
global materials, recipes, distance
26+
needed = {'FUEL': fuel}
27+
while len(needed) > 1 or 'ORE' not in needed:
28+
material = max(needed, key=lambda x: distance[x])
29+
quantity = needed[material]
30+
del needed[material]
31+
if material == 'ORE':
32+
needed[material] = quantity
33+
continue
34+
base_quantity, ingredients = recipes[material].values()
35+
for a, b in ingredients.items():
36+
if a not in needed:
37+
needed[a] = 0
38+
needed[a] += math.ceil(quantity/base_quantity)*b
39+
return needed['ORE']
40+
41+
def search_fuel_target(ore):
42+
one_unit = required_ore(1)
43+
target = ore//one_unit
44+
used_ore = required_ore(target)
45+
while True:
46+
target += (ore-used_ore)//one_unit + 1
47+
used_ore = required_ore(target)
48+
if used_ore > ore:
49+
break
50+
return target - 1
51+
52+
53+
print('Part 1:', required_ore(1))
54+
print('Part 2:', search_fuel_target(1000000000000))

day14/problem.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ Here are some larger examples:
8181
121 ORE => 7 VRPVC
8282
7 XCVML => 6 RJRHP
8383
5 BHXH, 4 VRPVC => 5 LTCX
84-
Given the list of reactions in your puzzle input, what is the minimum amount of ORE required to produce exactly 1 FUEL?
84+
Given the list of reactions in your puzzle input, what is the minimum amount of ORE required to produce exactly 1 FUEL?
85+
86+
--- Part Two ---
87+
After collecting ORE for a while, you check your cargo hold: 1 trillion (1000000000000) units of ORE.
88+
89+
With that much ore, given the examples above:
90+
91+
The 13312 ORE-per-FUEL example could produce 82892753 FUEL.
92+
The 180697 ORE-per-FUEL example could produce 5586022 FUEL.
93+
The 2210736 ORE-per-FUEL example could produce 460664 FUEL.
94+
Given 1 trillion ORE, what is the maximum amount of FUEL you can produce?

0 commit comments

Comments
 (0)