Guest User

Untitled

a guest
Oct 20th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. data Position = Position Int Int Direction
  2. deriving (Show, Eq, Read)
  3. data Direction = N | E | W | S
  4. deriving (Show, Eq, Enum, Bounded, Read)
  5. data Command = L | R | M
  6. deriving (Show, Eq, Enum, Bounded, Read)
  7.  
  8. data DeltaPosition = DeltaPosition Int Int
  9.  
  10. data State = State {
  11. leftDirection :: Direction,
  12. rightDirection :: Direction,
  13. delta :: DeltaPosition
  14. }
  15.  
  16. toPosition input = (read ("Position " ++ input)) :: Position
  17. toCommands input = map (\i -> (read [i]) :: Command) input
  18.  
  19. state N = (State W E (DeltaPosition 0 (-1)))
  20. state W = (State S N (DeltaPosition (-1) 0))
  21. state S = (State E W (DeltaPosition 0 1))
  22. state E = (State N S (DeltaPosition 1 0))
  23.  
  24. add (Position x y d) (DeltaPosition x' y') = Position (x + x') (y + y') d
  25.  
  26. execute (Position x y d) L = Position x y (leftDirection (state d))
  27. execute (Position x y d) R = Position x y (rightDirection (state d))
  28. execute p@(Position x y d) M = add p (delta (state d))
  29.  
  30. route position commands = foldl execute (toPosition position) (toCommands commands)
  31.  
  32. -- route "1 2 N" "LRLRM"
Add Comment
Please, Sign In to add comment