Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rotate :: ( Int, Int ) -> Char -> ( Int, Int )
- rotate ( x, y ) 'L' = ( negate y, x )
- rotate ( x, y ) 'R' = ( y, negate x )
- rotate ( x, y ) c = ( x, y )
- type State = ( ( Int, Int ), ( Int, Int ) )
- step :: State -> String -> State
- step ( dir, path ) instruction =
- let newDir = rotate dir $ head instruction
- len = read $ tail instruction
- in ( newDir, ( fst path + len * fst newDir, snd path + len * snd newDir ) )
- getPath ( x, y ) = abs x + abs y
- calc :: String -> ( Int, Int )
- calc = snd . foldl step ( ( 1, 0 ), ( 0, 0 ) ) . map ( takeWhile ( /= ',' ) ) . words
- main = getLine >>= ( \s -> putStrLn $ show $ getPath $ calc s )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement