public
anonymous / RollerCoaster.hs
Created

Just brute force, but it works

  • Download Gist
RollerCoaster.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.