Created
February 1, 2010 06:39
-
-
Save arocks/291497 to your computer and use it in GitHub Desktop.
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 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