Skip to content

Instantly share code, notes, and snippets.

@Denommus
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save Denommus/10691765 to your computer and use it in GitHub Desktop.

Select an option

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
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