Under construction

Still being built. Things may change

Punchy

Interview

Time limit2sMemory limit512 MB

Summary
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 00.

There is exactly one instruction per line. An instruction begins with an integer from 11 to 77. 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, XX and YY each denote a variable (A or B); wherever a second operand appears it may instead be a literal integer.

  • 1 X n — set variable XX to the integer nn.
  • 2 X — output the value of variable XX.
  • 3 X Y — compute X+YX + Y and store the result in XX.
  • 4 X Y — compute X×YX \times Y and store the result in XX.
  • 5 X Y — compute X−YX - Y and store the result in XX.
  • 6 X Y — compute the integer quotient of X÷YX \div Y, discarding the remainder, and store it in XX.
  • 7 — stop execution.

No division instruction divides by zero, and every computed or stored value stays within −10000-10000 to 1000010000.

Division truncates toward zero: −3÷2-3 \div 2 and 3÷−23 \div -2 both have quotient −1-1, while −3÷−2-3 \div -2 has quotient 11.

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.

Examples4

  1. Example 1

    Input
    1 A 3
    1 B 4
    2 B
    2 A
    3 A B
    2 A
    5 A A
    2 A
    2 B
    7
    
    Expected output
    4
    3
    7
    0
    4
    
  2. Example 2

    Input
    2 A
    2 B
    7
    
    Expected output
    0
    0
    
  3. Example 3

    Input
    1 A 2
    1 B 3
    3 A B
    2 A
    4 A B
    2 A
    5 A B
    2 A
    6 A B
    2 A
    7
    
    Expected output
    5
    15
    12
    4
    
  4. Example 4

    Input
    1 A 5
    1 B 10
    5 A B
    2 A
    3 A B
    2 A
    7
    
    Expected output
    -5
    5