-
-
Save nishidy/335ff29586ed959697ed56e68187cb95 to your computer and use it in GitHub Desktop.
Google Code Jam 2016 Qualification Round : Problem B. Revenge of the Pancakes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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