Skip to content

anonymous /RollerCoaster.hs

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Just brute force, but it works
import Text.Printf
-- Should be in some library
blockBy _ [] = []
blockBy n xs = ys : blockBy n zs where (ys, zs) = splitAt n xs
main = interact $
unlines .
zipWith (printf "Case #%d: %.0f") [1::Int ..] .
map (\ [[r,k,_], xs] -> solve r k xs) .
blockBy 2 .
map (map read . words) .
tail .
lines
solve rounds seats xs = loop 0 rounds maxGroup seats (cycle xs)
where maxGroup = length xs
loop :: Double -> Int -> Int -> Int -> [Int] -> Double
loop t 0 _ _ _ = t
loop t r 0 _ q = loop t (r-1) maxGroup seats q
loop !t !r !g !k (x:q) | x <= k = loop (t+fromIntegral x) r (g-1) (k-x) q
| otherwise = loop t (r-1) maxGroup seats (x:q)
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.