#P1322. Turtle Distance Calculation in Logo Language

    ID: 14609 Type: Default 1000ms 256MiB

Turtle Distance Calculation in Logo Language

Turtle Distance Calculation in Logo Language

The Logo programming language can control a turtle that moves on the screen. In this problem, the turtle moves only along a straight line, and you are given a sequence of Logo commands. There are only three commands allowed:

  • FD X: move forward X units.
  • BK X: move backward X units.
  • REPEAT N [ ... ]: repeat the commands inside the square brackets N times.

For example, the command FD 100 moves the turtle 100 units away from the starting point, while FD 100 BK 150 results in a displacement of 50 units (absolute value). Note that the commands can be nested arbitrarily.

Assume that the turtle can move far enough (up to \(10^9\) units in any direction). Your task is to output the absolute distance from the starting position after executing all the commands.

inputFormat

A single line containing a sequence of Logo commands. The commands may contain nested REPEAT blocks. A sample command is: REPEAT 5[FD 50 REPEAT 10[FD 100]].

outputFormat

Output the absolute distance of the turtle from its original starting position as an integer.

sample

FD 100
100