changeset 25:5812985ef721 draft default tip

optim.hs: finalCapital WIP broken mess
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 31 Mar 2015 16:21:59 -0400
parents 1bebd7b76bac
children
files optim.hs
diffstat 1 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/optim.hs
+++ b/optim.hs
@@ -13,9 +13,37 @@
                  ,capital  :: Number
                  ,days     :: Number} deriving Show
 
+type Plan = [Bool]
+
 numSplit :: String -> [[Number]]
 numSplit str = [[read num | num <- words line] | line <- lines str]
 
+
+finalCapital :: Case -> [Machine] -> Plan -> Maybe Number
+finalCapital thecase machines plan =
+  alldays + finalday
+  where
+    (alldays, finalslot) =
+      foldl' addCapital (Just (capital thecase), Nothing) $ zip plan machines
+    finalday =
+      if finalslot == Nothing then
+        0
+      else
+        (days thecase + day finalslot)*profit finalslot + sell finalslot
+
+    addCapital :: (Maybe Number, Maybe Machine)
+                  -> (Bool, Machine)
+                  -> (Maybe Number, Maybe Machine)
+    addCapital (Nothing, _) _ = (Nothing, Nothing)
+    addCapital (Just currcapital, Nothing) (action, machine)
+      | currcapital < buy machine = Nothing
+      | otherwise                 = Just 2
+
+    addCapital (Just currcapital, Just slot) (action, machine)
+      | action = Just (currcapital + profit machine)
+      | otherwise = Just 0
+
+
 makeMachine :: Number -> [Number] -> Machine
 makeMachine days line = Machine mDay mBuy mSell mProfit mMaxprofit
   where