Skip to content

Instantly share code, notes, and snippets.

Created January 23, 2011 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/791960 to your computer and use it in GitHub Desktop.
Save anonymous/791960 to your computer and use it in GitHub Desktop.
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