# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1426041105 14400 # Node ID b5cbd6bdb6115416027fc82341f4a5e11a3481fd # Parent af2598b58791ff6d6282549bce28ced86b46b2b1 final_capital: first sell the machine before checking balance to buy (bug) diff --git a/optim.py b/optim.py --- a/optim.py +++ b/optim.py @@ -58,13 +58,7 @@ for (action, machine) in sorted(zip(plan, machines), key = lambda x: x[1].day): if action: - # We attempt to buy a new machine - if capital < machine.buy: - raise InvalidPlan("Not enough capital at day %d: " - "machine cost is %d, and capital is %d" - % (machine.day, machine.buy, capital)) - - capital -= machine.buy + # We first sell the old machine, if any if slot: if machine.day == slot.day: raise InvalidPlan("Cannot buy two machines on the same day: %d" @@ -76,6 +70,13 @@ capital += days_used*slot.profit capital += slot.sell + # Then we try to buy the new one + if capital < machine.buy: + raise InvalidPlan("Not enough capital at day %d: " + "machine cost is %d, and capital is %d" + % (machine.day, machine.buy, capital)) + + capital -= machine.buy slot = machine # We account for the final machine, if we have it.