def test_fuel_for_ore_stock_secant(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, _ = secant(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_ore_required_topological(text, expected):
    reactions = parse_reactions(text)
    ordering = topological_order(reactions)
    ore, _ = ore_required_topological(reactions, ordering, 1)
    assert ore == expected
Esempio n. 4
0
example = """5 ORE => 7 A
3 A => 10 B
1 A, 3 B => 1 C
5 C, 2 B => 1 FUEL"""

if __name__ == "__main__":
    print("Day 14")
    if len(sys.argv) == 2:
        try:
            text = open(sys.argv[1]).read().strip()
        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)
def test_ore_required_stock(text, expected):
    reactions = parse_reactions(text)
    ore, _ = ore_required_stock(reactions, 1)
    assert ore == expected