changeset 8:b5cbd6bdb611 draft

final_capital: first sell the machine before checking balance to buy (bug)
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 10 Mar 2015 22:31:45 -0400
parents af2598b58791
children 69e65d1d94af
files optim.py
diffstat 1 files changed, 8 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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.