Skip to content

Commit

Permalink
Day 14 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jcisio committed Dec 14, 2019
1 parent 5f08e33 commit 873c766
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
45 changes: 31 additions & 14 deletions day14/d14.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,35 @@
continue
distance[material] = max([distance[i] for i in recipes[material]['ingredients'].keys()]) + 1

needed = {'FUEL': 1}
while len(needed) > 1 or 'ORE' not in needed:
material = max(needed, key=lambda x: distance[x])
quantity = needed[material]
del needed[material]
if material == 'ORE':
needed[material] = quantity
continue
base_quantity, ingredients = recipes[material].values()
for a, b in ingredients.items():
if a not in needed:
needed[a] = 0
needed[a] += math.ceil(quantity/base_quantity)*b

print('Part 1:', needed['ORE'])
def required_ore(fuel):
global materials, recipes, distance
needed = {'FUEL': fuel}
while len(needed) > 1 or 'ORE' not in needed:
material = max(needed, key=lambda x: distance[x])
quantity = needed[material]
del needed[material]
if material == 'ORE':
needed[material] = quantity
continue
base_quantity, ingredients = recipes[material].values()
for a, b in ingredients.items():
if a not in needed:
needed[a] = 0
needed[a] += math.ceil(quantity/base_quantity)*b
return needed['ORE']

def search_fuel_target(ore):
one_unit = required_ore(1)
target = ore//one_unit
used_ore = required_ore(target)
while True:
target += (ore-used_ore)//one_unit + 1
used_ore = required_ore(target)
if used_ore > ore:
break
return target - 1


print('Part 1:', required_ore(1))
print('Part 2:', search_fuel_target(1000000000000))
12 changes: 11 additions & 1 deletion day14/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ Here are some larger examples:
121 ORE => 7 VRPVC
7 XCVML => 6 RJRHP
5 BHXH, 4 VRPVC => 5 LTCX
Given the list of reactions in your puzzle input, what is the minimum amount of ORE required to produce exactly 1 FUEL?
Given the list of reactions in your puzzle input, what is the minimum amount of ORE required to produce exactly 1 FUEL?

--- Part Two ---
After collecting ORE for a while, you check your cargo hold: 1 trillion (1000000000000) units of ORE.

With that much ore, given the examples above:

The 13312 ORE-per-FUEL example could produce 82892753 FUEL.
The 180697 ORE-per-FUEL example could produce 5586022 FUEL.
The 2210736 ORE-per-FUEL example could produce 460664 FUEL.
Given 1 trillion ORE, what is the maximum amount of FUEL you can produce?

0 comments on commit 873c766

Please sign in to comment.