Hide

Problem G
RC Ranging

For Ivan’s birthday, he received a new remote-controlled toy car. Although it is somewhat limited in how it can move, it does have one feature he really likes: it’s programmable! The programs are plain-text files containing instructions in a simple programming language. Such a driving program may even allow the car to travel outside the range of the controller. It doesn’t need to communicate with the controller as long as it is following the instructions it was given.

One of Ivan’s friends wants to try writing some programs for the car, but Ivan is a little concerned. It’s fine if the programs take the car outside the range of the controller temporarily so long as it comes back in range before it is done. If the program ends while the car is still outside the range of the controller, then Ivan won’t be able to use the controller to bring it back. He wants you to look at a program for the car and decide where the car will end up after it is done following the instructions.

The car can only move in two different ways: it can drive forward in a straight line, and it can rotate in place. There are three primitive commands that can be used to change the car’s position.

FORWARD num — moves the car forward the specified number of meters

LEFT — turns the car counter-clockwise in place by 90 degrees

RIGHT — turns the car clockwise in place by 90 degrees

In order to avoid copying and pasting instructions, programs have two different ways to reuse instructions. The first is to put the instructions inside a repeat statement. This allows the instructions to be repeated a certain number of times.

REPEAT num instructions... END — repeat instructions num times

The second method of instruction reuse is to define a name for a group of instructions—that is, a named procedure or function. Once a group of instructions has been named, it can be executed at any time by referring to the name.

TO name instructions... END — associate name with the given instructions

DO name — perform the instructions defined for name

Note that both REPEAT and DO are considered instructions. Therefore, these can both appear inside a REPEAT statement or a TO statement. However, the TO statement can only appear at the top level of the program; it can not appear inside a REPEAT or TO statement.

We can represent all this information formally using Backus-Naur Form (BNF), extended with a repetition operator—X+ means “a sequence of one or more copies of X”.

<program> ::= (<definition> | <instruction>)+

<definition> ::= "TO" <identifier> <instruction>+ "END"

<instruction> ::=
    "FORWARD" <number>
  | "LEFT"
  | "RIGHT"
  | "REPEAT" <number> <instruction>+ "END"
  | "DO" <identifier>

Here <identifier> denotes any nonempty sequence of alphabetic characters (i.e. uppercase and lowercase English letters), except for the special reserved words TO, FORWARD, LEFT, RIGHT, REPEAT, DO, and END, which will not be used as procedure names. Note that programs are case-sensitive, so, for example, CAR and car are different names. As another example, End may be used as a procedure name even though END will not.

<number> denotes a nonnegative integer, written using a nonempty sequence of digits (with no unnecessary leading 0). All numbers will be between 0 and 1000, inclusive.

Whitespace (space and newline characters) in the input program should be ignored. All tokens in the program will be separated by at least one whitespace character.

Input

The input consists of a single, syntactically correct program, with a total length of at most $2000$ characters (including whitespace). It is guaranteed that any procedure names will be defined with a TO statement prior to being referenced by DO, and moreover that a procedure will never call itself—that is, a definition TO <name> will never contain DO <name>. Also, any name defined with TO will never be redefined by another TO later.

You should assume that the car begins at position $(0, 0)$, facing in the direction of the positive $y$-axis (that is, north), with the positive $x$-axis pointing to the east.

It is guaranteed that executing the program will cause the car to perform at most $10^5$ total primitive commands (that is, FORWARD, LEFT, or RIGHT commands).

Output

Output two space-separated integers giving the final $x$- and $y$-coordinates of the car after executing the program.

Sample Input 1 Sample Output 1
FORWARD 10
0 10
Sample Input 2 Sample Output 2
REPEAT 7
  LEFT
  FORWARD 1
END
0 -1
Sample Input 3 Sample Output 3
FORWARD 1
RIGHT
TO zig
  FORWARD 1
  RIGHT
  FORWARD 1
  LEFT
END

TO zag
  LEFT
  DO zig
  RIGHT
  DO zig
  LEFT
END

REPEAT 10
  DO zig
  DO zag
  FORWARD 1
END
3 4
Sample Input 4 Sample Output 4
TO do REPEAT 3 REPEAT 5 FORWARD 1 END RIGHT END END DO do DO do
5 5

Please log in to submit a solution to this problem

Log in