If someone facing north at point A in this grid wanted directions to follow the green path (as they can only follow gridlines) to point B you might tell them:
Go North, North, West, East, East, South, East, East
.
or equivalently
Go Forward, Forward, Left, Back, Forward, Right, Left, Forward
.
(Where a command of Right, Left, or Back implicitly means turn in that direction, then go forward.)
Write a function with one argument that translates between these absolute and relative directions along the same path, not just to the same point. Assume the directed person always starts facing north.
If the argument is a string of the letters NSEW
, return the equivalent relative directions.
e.g. f("NNWEESEE")
returns the string FFLBFRLF
.
If the argument is a string of the letters FBLR
, return the equivalent absolute directions.
e.g. f("FFLBFRLF")
returns the string NNWEESEE
.
The empty string yields itself. Assume no other input cases.
If your language does not have functions or strings use whatever seems most appropriate.
The shortest code in bytes wins.
R
equalsE
at the start. \$\endgroup\$