Skip to content

Instantly share code, notes, and snippets.

@petermarks
Created January 16, 2011 22:13
Show Gist options
  • Save petermarks/782199 to your computer and use it in GitHub Desktop.
Save petermarks/782199 to your computer and use it in GitHub Desktop.
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