Skip to content

Instantly share code, notes, and snippets.

@nishidy
Created May 6, 2016 13:32
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 nishidy/335ff29586ed959697ed56e68187cb95 to your computer and use it in GitHub Desktop.
Save nishidy/335ff29586ed959697ed56e68187cb95 to your computer and use it in GitHub Desktop.
Google Code Jam 2016 Qualification Round : Problem B. Revenge of the Pancakes
import Debug.Trace
testloop :: Integer -> Integer -> IO String
testloop i t | i>t = return ""
testloop i t = getLine >>= testnum 0 >>= \x->return ("Case #"++(show i)++": "++x) >>= putStrLn >> testloop (i+1) t
testnum :: Integer -> String -> IO String
--testnum n s = flipcakes s >>= \x-> trace(x) (oneside n x)
testnum n s = case oneside n s of
Just n | any (=='+') s == True -> return $ show n
Just n -> return $ show $ n+1
Nothing -> flipcakes s >>= testnum (n+1)
flipcakes :: String -> IO String
flipcakes s@(x:xs) = return $ (flipcake $ reverse $ takeWhile(==x) s) ++ (dropWhile(==x) s)
oneside :: Integer -> String -> Maybe Integer
oneside n (x:xs) | all (==x) xs == True = return n
oneside n (x:xs) = Nothing
flipcake :: String -> String
flipcake (x:xs) = y:ys
where
y = case x of
'+' -> '-'
'-' -> '+'
ys = flipcake xs
flipcake [] = []
main = getLine >>= \t-> testloop 1 $ read t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment