#P4639. Simulate the SH Language
Simulate the SH Language
Simulate the SH Language
The SH language is a simple programming language that, like Pascal or C/C++, uses registers and commands. There are \(26\) registers denoted by the capital letters A, B, …, Z. The first line of the program contains the initial values of these \(26\) registers separated by spaces. From the second line onward, each line contains a command.
The SH language supports three commands:
- ADD command – Format:
ADD_R1_R2
. It adds the value of register R2 to register R1. There are no restrictions for this command. - GOTO command – Format:
IF_R_< I1_ GOTO _LINE_ I2
(underscores represent spaces in the actual language). It checks if the value in register R is less than the immediate value \(I1\). If true, the program jumps to line number \(I2\) (with \(I2 \ge 2\)); otherwise, the program continues with the next line. This command appears at most once and, if present, it appears only in a line that is after the target line \(I2\), i.e. the jump is backwards. - PRINT command – Format:
PRINT_R
. It prints the value of register R. This command appears exactly once and only as the last line of the program.
You are given a SH language program. Your task is to simulate the program and output the value printed by the PRINT
command.
inputFormat
The input consists of multiple lines. The first line contains \(26\) space-separated integers representing the initial values of registers A through Z. Each subsequent line contains one command in the SH language as described above.
outputFormat
Output the integer value printed by the PRINT
command.
sample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
ADD_A_B
PRINT_A
3