Skip to content

Instantly share code, notes, and snippets.

@berdario
Last active August 29, 2015 14:06
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 berdario/6c33b11a137386af62e1 to your computer and use it in GitHub Desktop.
Save berdario/6c33b11a137386af62e1 to your computer and use it in GitHub Desktop.
import Data.Functor.Identity (runIdentity)
import Control.Monad (replicateM)
import Control.Monad.State.Lazy (state, evalState)
getInts :: IO [Int]
getInts = fmap (map read . words) getLine
f = do
n:q:_ <- getInts
els <- replicateM n getInts
vals <- getInts
queries <- replicateM q getInts
print (els, vals, queries)
readInts :: String -> [Int]
readInts = map read . words
readIntsLines (x, y) = (map readInts x, y)
f2 s = show (els, vals, queries)
where
(n:q:_, s') = (readInts $ head s, tail s)
(els, s'') = readIntsLines $ splitAt n s'
(vals, s''' ) = (readInts $ head s'', tail s'')
(queries, _) = readIntsLines $ splitAt q s'''
f3 s = runIdentity $ do
(n:q:_, s) <- return $ (readInts $ head s, tail s)
(els, s) <- return $ (map readInts $ take n s, drop n s)
(vals, s) <- return $ (readInts $ head s, tail s)
queries <- return $ map readInts $ take q s
return $ show (els, vals, queries)
readLines n = fmap (map readInts) $ state (splitAt n)
f4 s = flip evalState s $ do
(n:q:_):_ <- readLines 1
els <- readLines n
vals:_ <- readLines 1
queries <- readLines q
return $ show (els, vals, queries)
f'= interact (f4 . lines)
main = f
-- expected output with dat as input:
-- ([[1,2,3],[4,5,6]],[7,8,9],[[10,11,12],[13,14,15],[16,17,18]])
2 3
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment