Skip to content

Instantly share code, notes, and snippets.

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