Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Theme Park (Naive)
module Main where
import Text.Printf
profit :: Integer -> Integer -> [Integer] -> Integer
profit cap rides groups
| sum groups <= cap = rides * sum groups
| otherwise = profit' cap rides (cycle groups)
where
profit' _ 0 _ = 0
profit' cap' rides' groups'@(g:gs)
| cap' < g = profit' cap (rides' - 1) groups'
| otherwise = g + profit' (cap' - g) rides' gs
processFile :: String -> String
processFile s = unlines $ zipWith (printf "Case #%d: %d") ([1..]::[Integer]) profits
where
cs = map (map read . words) . drop 1 . lines $ s
profits = chop processCase cs
processCase :: [[Integer]] -> (Integer, [[Integer]])
processCase ( [rides, cap, _ ] : groups : rest) =
(profit cap rides groups, rest)
processCase _ = error "Invalid Format!!!!!"
chop :: ([a] -> (b , [a])) -> [a] -> [b]
chop _ [] = []
chop f xs = y : chop f xs'
where (y, xs') = f xs
main :: IO ()
main = interact processFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.