Skip to content

Instantly share code, notes, and snippets.

@RationalAsh
Created April 13, 2019 06:53
Show Gist options
  • Save RationalAsh/ba917a855dc63400245478ad4a03b1e5 to your computer and use it in GitHub Desktop.
Save RationalAsh/ba917a855dc63400245478ad4a03b1e5 to your computer and use it in GitHub Desktop.
Haskell Solution to Google Code Jam 2019 Qualifying Round Problem 2.
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