TL;DR: Given an array of chars and a robot in a starting position of the array, write an algorithm than can read a string with movements (
Ffor "go forward",
Rfor "rotate 90 degrees right" and
Lfor "rotate 90 degrees left") and calculate the ending position of the robot. More details in the complete text.
We have at home a very simple programmable device for kids: a small vehicle with buttons to make the vehicle go forward, turn 90 degrees left or turn 90 degrees right. Something similar to this:
We also have a foam mat with letters like this:
The purpose of all this is to teach the kids both the alphabet and the rudiments of programming, all at once.
Suppose we have randomly arranged our foam mat like this:
+---+---+---+---+---+---+---+ | E | R | L | B | I | X | N | +---+---+---+---+---+---+---+ | O | A | Q | Y | C | T | G | +---+---+---+---+---+---+---+ | F | W | H | P | D | Z | S | +---+---+---+---+---+---+---+ | K | V | U | M | J | +---+---+---+---+---+ | | +---+
Suppose also we have modified the vehicle so that when we program a "go forward" command, the vehicle goes forward exactly size of one square in the mat. So, if the vehicle is in the
U square and goes north, it stops exactly in the
The instructions are all given to the vehicle before it starts to move, and those are:
F: The vehicle goes forward into the next square.
R: The vehicle turns 90 degrees right in its place (no further movement).
L: The vehicle turns 90 degrees left in its place (no further movement).
Once the instructions are given, you can press the "GO" button and send the vehicle to a given position as it will follow every instruction in the given order. So, you can tell the kid to insert the needed instructions for the vehicle to go to a given letter.
You must write the shortest program/function that processes a
string (input parameter) with a set of instructions and calculates the letter the vehicle stops over (output
- The vehicle always starts at the blank square at the bottom, and facing north (towards the
- The input string will contain only the letters
G(for the "go" button). You can use lowercase letters for the mat and the instructions, if you prefer so.
- The algorithm must obey every instruction in the string before the first
G(every instruction after that is ignored as the vehicle has started moving).
- If the vehicle goes out of the mat at any given moment (even if the input string has not been completely processed), the algorithm must return the string
Out of mat.
- If not, the algorithm must return the letter the vehicle has stopped over. The starting point counts as a
char (or an empty string).
Input: FFG Output: P Input: FRFRFG Output: Out of mat Input: RRFFG Output: Out of mat Input: FFFRFFLFG Output: X Input: FFFRFFLF Output: <-- Nothing or a whitespace (the robot has not started moving) Input: FFFRRFFFG Output: <-- Nothing or a whitespace (the robot has returned to the starting point) Input: RRRRRLFFFLFFRFRFGFFRRGRFF Output: L (Everything after the first G is ignored)
This is code-golf, so may the shortest program for each language win!