Last active
August 29, 2015 13:59
-
-
Save Denommus/10691765 to your computer and use it in GitHub Desktop.
Solution for the second problem of the Google Code Jam 2014 Qualification Round
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import System.Environment | |
| import Control.Monad | |
| import System.IO | |
| calculateTime :: Double -> Double -> Double -> Double | |
| calculateTime c f x = loop 2 | |
| where loop rate | |
| | outright <= buying = outright | |
| | otherwise = waiting + loop newRate | |
| where waiting = c / rate | |
| outright = x / rate | |
| buying = x / newRate + waiting | |
| newRate = rate + f | |
| printResults :: Show a => Handle -> (a, String) -> IO () | |
| printResults output (i, cs) = hPutStrLn output $ "Case #" ++ show i ++ ": " | |
| ++ show (calculateTime c f x) | |
| where [c,f,x] = map read $ words cs | |
| main :: IO () | |
| main = do | |
| content <- getArgs >>= readFile . head | |
| let cases : eachCase = lines content | |
| withFile "output" WriteMode (\out -> zip [1..(read cases :: Int)] eachCase `forM_` printResults out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment