Safe Haskell | None |
---|---|
Language | Haskell2010 |
This defines a Google Code Jam monad with a handful of combinators for simple parsing of input files.
The test cases are solved in parallel.
Although the parsing of the input cannot happen in parallel (lines are consumed one by one, and input of the nexta example depends on how much the current has consumed), the actual work can be automatically parallelized, if the number of lines to consume does not depend on the result of the main workload of each case.
This is demonstrated by the following example:
parallel test mainP = mainId $ do r <- return . length . show . product . enumFromTo (1::Integer) . read =<< cLine if not parallel && r == 1 then return =<< cLine else return $ show r where parallel = False
input file:
2 200000 200000 200000 200000
- newtype GCJ m a = GCJ {}
- cLine :: Monad m => GCJ m String
- cLines :: Monad m => Int -> GCJ m [String]
- cBlock :: Monad m => Int -> ([String] -> a) -> GCJ m (Int, a)
- type Runner m = GCJ m String -> [String] -> IO ()
- mainId :: Runner Identity
- mainIO :: Runner IO
- mainM :: Monad m => (forall a. m a -> IO a) -> Runner m
Documentation
data type for Google Code Jam Solution Program
It has to be a Monad (e.g. to implement cBlock).
Next action has to be able to depend on state.
Combinators
cBlock :: Monad m => Int -> ([String] -> a) -> GCJ m (Int, a) Source #
consumes, transforms and returns next block:
a block consists of first line: n, then (n-m) lines