#K82147. Final Coordinates Calculation
Final Coordinates Calculation
Final Coordinates Calculation
You are given a sequence of instructions that tell you to move from the origin (0,0) in a 2D plane. Each instruction consists of a direction and a number of steps. The valid directions are up, down, left, and right. Your task is to compute the final coordinates after executing all the instructions.
The movement rules are defined as follows:
- right: move along the positive x-axis
- left: move along the negative x-axis
- up: move along the positive y-axis
- down: move along the negative y-axis
You can express the change in coordinates for each instruction mathematically as:
[ \begin{aligned} \Delta x &= \begin{cases} +\text{steps} & \text{if direction is } \text{right}\ -\text{steps} & \text{if direction is } \text{left}\ 0 & \text{otherwise} \end{cases}\[1ex] \Delta y &= \begin{cases} +\text{steps} & \text{if direction is } \text{up}\ -\text{steps} & \text{if direction is } \text{down}\ 0 & \text{otherwise} \end{cases} \end{aligned} ]
After processing all instructions, output the final coordinates (x, y).
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
- The first line contains an integer n representing the number of instructions.
- Each of the next n lines contains a string and an integer separated by a space. The string represents the direction and the integer represents the number of steps.
Example:
4 right 10 up 5 left 7 down 2
outputFormat
The output should be written to standard output (stdout) and consists of two integers separated by a space that represent the final x and y coordinates.
Example Output:
3 3## sample
4
right 10
up 5
left 7
down 2
3 3