Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. rotate :: ( Int, Int ) -> Char -> ( Int, Int )
  2. rotate ( x, y ) 'L' = ( negate y, x )
  3. rotate ( x, y ) 'R' = ( y, negate x )
  4. rotate ( x, y ) c = ( x, y )
  5.  
  6. type State = ( ( Int, Int ), ( Int, Int ) )
  7. step :: State -> String -> State
  8. step ( dir, path ) instruction =
  9. let newDir = rotate dir $ head instruction
  10. len = read $ tail instruction
  11. in ( newDir, ( fst path + len * fst newDir, snd path + len * snd newDir ) )
  12.  
  13. getPath ( x, y ) = abs x + abs y
  14.  
  15. calc :: String -> ( Int, Int )
  16. calc = snd . foldl step ( ( 1, 0 ), ( 0, 0 ) ) . map ( takeWhile ( /= ',' ) ) . words
  17.  
  18.  
  19. main = getLine >>= ( \s -> putStrLn $ show $ getPath $ calc s )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement