public
Created

Theme Park (Naive)

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.