Created
April 13, 2019 06:53
-
-
Save RationalAsh/ba917a855dc63400245478ad4a03b1e5 to your computer and use it in GitHub Desktop.
Haskell Solution to Google Code Jam 2019 Qualifying Round Problem 2.
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 Control.Monad | |
sampleMoves = "EESSSESE" | |
applyMove :: (Int, Int) -> Char -> (Int, Int) | |
applyMove (row, col) move | |
| move == 'S' = (row+1, col) | |
| move == 'E' = (row, col+1) | |
| otherwise = (row, col) | |
flipMove :: Char -> Char | |
flipMove c | |
| c == 'S' = 'E' | |
| c == 'E' = 'S' | |
| otherwise = c | |
getTrajectory :: String -> [(Int, Int)] | |
getTrajectory moves = scanl applyMove (1, 1) moves | |
solveCase :: (Int, Int, String) -> (Int, String) | |
solveCase (i, n, lydiasMoves) | |
| n == 2 = (i, reverse lydiasMoves) | |
| otherwise = (i, map flipMove lydiasMoves) | |
caseOutput :: (Int, String) -> IO () | |
caseOutput (i, str) = putStrLn ("Case #" ++ show i ++ ": " ++ str) | |
readCase :: Int -> IO (Int, Int, String) | |
readCase i = do | |
nstr <- getLine | |
let n = (read nstr) :: Int | |
moves <- getLine | |
return (i, n, filter (\c -> (c /= '\r') && (c /= '\n')) moves) | |
main = do | |
t <- getLine | |
let tn = (read t) :: Int | |
cases <- forM [1..tn] readCase | |
let solutions = map solveCase cases | |
mapM_ caseOutput solutions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment