Skip to content

Instantly share code, notes, and snippets.

@nishidy nishidy/b.hs
Created May 6, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.