Skip to content

Instantly share code, notes, and snippets.

@arocks
Created February 1, 2010 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arocks/291497 to your computer and use it in GitHub Desktop.
Save arocks/291497 to your computer and use it in GitHub Desktop.
import Data.List
dirs = "NESW"
shifts 0 = (0, 1)
shifts 1 = (1, 0)
shifts 2 = (0, -1)
shifts 3 = (-1, 0)
instrn (x, y, a) 'R' = (x, y, mod (a + 1) 4)
instrn (x, y, a) 'L' = (x, y, mod (a - 1 + 4) 4)
instrn (x, y, a) 'M' = (x+fst (shifts a), y+snd (shifts a), a)
showpos (x, y, a) = show x ++ " " ++ show y ++ " " ++ [dirs !! a]
finddir dirchar =
case elemIndex dirchar dirs of
Nothing -> error "invalid direction"
Just position -> position
readpos line = (x, y, a)
where a = finddir $ head $ drop 1 line3
[(y,line3)] = reads line2 :: [(Integer, String)]
[(x,line2)] = reads line :: [(Integer, String)]
robo = do
posn <- getLine
instrns <- getLine
putStrLn (showpos (foldl instrn (readpos posn) instrns))
robo
main = do
skip <- getLine -- Skip reading the grid size
robo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment