#C1444. Robot Manhattan Distance
Robot Manhattan Distance
Robot Manhattan Distance
Given a string of commands consisting only of the characters U
, D
, L
, and R
, simulate the movement of a robot starting at the origin (0, 0) on the Cartesian plane. Each command moves the robot one unit in the respective direction: up, down, left, or right.
The task is to output the Manhattan distance from the robot's final position to the origin, where the Manhattan distance is defined as: $$d(x,y) = |x| + |y|.$$
For example, if the robot receives the command string RUULDD
, it will eventually return to the starting point, giving a distance of 0. If it receives UUUU
, the robot moves 4 units up, resulting in a distance of 4.
Read the command string from standard input and output the resulting Manhattan distance to standard output.
inputFormat
The input consists of a single line containing a non-empty string of characters. Each character will be one of 'U', 'D', 'L', or 'R', and represents a movement command for the robot.
This input is provided via standard input (stdin).
outputFormat
Output a single integer, which is the Manhattan distance from the final position of the robot to the origin (0, 0). Recall that the Manhattan distance is calculated as: $$|x| + |y|.$$
This output should be printed to standard output (stdout).
## sampleRUULDD
0