Given a string representing a series of aircraft marshalling hand signals, write a function or program to calculate the final position of an aircraft following these signals.
N.B.: Co-ordinates in this challenge are represented as a pair of Cartesian co-ordinates, plus a compass direction heading:
(x, y, h) where
x is the x-coordinate,
y is the y-coordinate, and
h is one of
You start with an aircraft at
(0, 0, N) on an imaginary grid, with engines off. Your input is a string containing comma-delimeted pairs of characters, where each pair represents one marshalling signal. You must follow each marshalling signal in turn, and output the co-ordinates in
(x, y, h) form of the aircraft's final position.
If a signal requires your aircraft to move, assume it moves one unit in the required direction for each signal of that type that it receives. If a signal requires your aircraft to turn, assume it turns 90 degrees in the required direction for each signal of that type that it receives.
An aircraft cannot move if its engines are off. If your aircraft's engines are off and you receive a movement/turn signal, do not apply the movement/turn.
Each marshalling signal is represented by one pair of characters. The first of the pair represents the position of the marshaller's left arm, from the aircraft's point of view, and the second the right arm from the same POV. This handy chart of signals may help.
o/ — START ENGINES (no movement, no turn) -/ — CUT ENGINES (no movement, no turn) -~ — TURN LEFT (no movement, left turn) ~- — TURN RIGHT (no movement, right turn) ~~ — COME FORWARD (forward movement, no turn) :: — MOVE BACK (backward movement, no turn) /\ — NORMAL STOP (no movement, no turn)
This is not the complete list of marshalling signals, but it's all you're required to support.
Input is a comma-delimeted string containing pairs of characters. This string will always be valid - you do not have to validate the input.
Output is a set of co-ordinates as described above. You can return this in any convenient format - if your language supports multiple return values, you may use that; alternatively, you can use a string (the brackets surrounding the co-ordinates are non-compulsory), array, tuple, list, or whatever else you find convenient. The only rule is that it must contain
h values, in that order.
Input — Output o/,~~,~~,~-,:: — (-1, 2, E) ::,~-,o/,/\,~~,-~,~~,~~,~~ — (-3, 1, W) o/,::,-/,~~,o/,~-,~~,~~,~- — (2, -1, S) o/,~-,~-,::,::,~-,~~,-~ — (-1, 2, S) ~-,~-,o/,::,::,-/,~~,-~ — (0, -2, N)