# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1427833319 14400 # Node ID 5812985ef721ccdd26fe78f62552e0432829421c # Parent 1bebd7b76bacb0b91ef42524120ae8c9eebe6816 optim.hs: finalCapital WIP broken mess diff --git a/optim.hs b/optim.hs --- 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