except: text = example else: text = example reactions = parse_reactions(text) print("\nPART I - ORE required to produce 1 FUEL") print("\nStock method:") ore_used, i = ore_required_stock(reactions, 1) print(f"ORE used to produce one FUEL: {ore_used}; Took {i} iterations.") print("\nTopological ordering method:") ordering = topological_order(reactions) ore_used, i = ore_required_topological(reactions, ordering, 1) print(f"ORE used to produce one FUEL: {ore_used}; Took {i} iterations.") print("\nPART II - FUEL produced with 1 trillion ORE") TRILLION = int(1E12) estimations, order = estimate_fuel_produced(reactions, TRILLION) print(f"Estimated between {estimations[0]} and {estimations[1]} FUEL.") print("\nBisect method:") def f(x, reactions, ordering): ore, _ = ore_required_topological(reactions, ordering, x) return ore ordering = topological_order(reactions) args = (reactions, ordering)
def f(x, reactions, ordering): ore, _ = ore_required_topological(reactions, ordering, x) return ore
def test_ore_required_topological(text, expected): reactions = parse_reactions(text) ordering = topological_order(reactions) ore, _ = ore_required_topological(reactions, ordering, 1) assert ore == expected