Punchy
InterviewTime limit2sMemory limit512 MB
Simulate a tiny two-variable language, running the given instructions in order until a stop opcode, and print each requested variable value.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Math
- Solved
- No attempts yet
Problem
In the early days of computing, a program was "punched" onto rectangular cards — one instruction per card. The deck was then fed into a card reader so the program could be read and executed. Fearing that a shuffled deck would ruin their program, students often wrapped an elastic band around the deck and carried it in a box.
Bill was less careful. He left his deck next to an open window, a gust of wind scattered the cards, and now his program is out of order. Bill gathers the cards in whatever order he happens to pick them up and runs the resulting program.
Read Bill's rearranged program and execute the instructions in the order they are given.
Input
Bill's language has only two variables, A and B, and seven kinds of instructions. Both variables start at .
There is exactly one instruction per line. An instruction begins with an integer from to . Depending on the opcode it may be followed by a variable name, which in turn may be followed by either a number or a variable.
Below, and each denote a variable (A or B); wherever a second operand appears it may instead be a literal integer.
1 X n— set variable to the integer .2 X— output the value of variable .3 X Y— compute and store the result in .4 X Y— compute and store the result in .5 X Y— compute and store the result in .6 X Y— compute the integer quotient of , discarding the remainder, and store it in .7— stop execution.
No division instruction divides by zero, and every computed or stored value stays within to .
Division truncates toward zero: and both have quotient , while has quotient .
Output
For each output instruction, print the value of the requested variable on its own line, in the order the instructions are executed. Stop as soon as the stop instruction (7) is read.