def test_fuel_for_ore_stock_bisect(text, expected): reactions = parse_reactions(text) def f(x, reactions): ore, _ = ore_required_stock(reactions, x) return ore target = int(1E12) interval = (1, int(1E10)) args = (reactions, ) result, _ = bisect(f, target, interval, args) assert result == expected
def test_fuel_for_ore_topological_secant(text, expected): reactions = parse_reactions(text) ordering = topological_order(reactions) def f(x, reactions, ordering): ore, _ = ore_required_topological(reactions, ordering, x) return ore target = int(1E12) interval = (1, int(1E10)) args = (reactions, ordering) result, _ = bisect(f, target, interval, args) assert result == expected
def test_bisect(function, target, interval, expected): result, _ = bisect(function, target, interval) assert result == expected
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) result, i = bisect(f, TRILLION, estimations, args) print(f"1 trillion ORE can produce: {result} FUEL; Took {i} iterations.") print("\nSecant method:") result, i = secant(f, TRILLION, estimations, args) print(f"1 trillion ORE can produce: {result} FUEL; Took {i} iterations.")