Under construction

Still being built. Things may change

Halt and Catch Fire

Time limit1sMemory limit2048 MB

Summary
Simulate a small assembly-like program with mov, add, sub, jeq, and hcf, then print $out or the machine state at the hcf instruction.
Level

Easy3 of 10

Topics
Simulation, Hash map
Solved
No attempts yet

Problem

Research has led to the creation of a new super fast CPU called the Fast Processing Chip (FPC). This product has a never-before-seen debugging function: an instruction called hcf, which gives more debugging power than any previously designed CPU.

The product is becoming popular quickly, and companies want to get their hands on the chip to start development right away. However, the chip is too expensive to hand to every developer.

You have been hired to design a program that runs as if it were a processor. Programmers can write programs with it and test them easily without needing the hardware.

Input

The first line contains one integer 1≤n≤10001 \leq n \leq 1000, the number of lines in the program.

The next nn lines each have three parts, separated by spaces:

  • The operation, always 3 characters.
  • An argument, which is either an integer −231≤a≤231−1-2^{31} \leq a \leq 2^{31}-1 or a variable name prefixed with $.
  • An argument, which is either an integer −231≤b≤231−1-2^{31} \leq b \leq 2^{31}-1 or a variable name prefixed with $.

There are five operations:

  • mov: Moves the value of the first argument to the second argument.
  • add: Adds the value of the second argument to the first argument and stores the result in $acc.
  • sub: Subtracts the value of the second argument from the first argument and stores the result in $acc.
  • jeq: Jumps to the instruction at the address given by the value of the second argument if the value of the first argument equals $cmp.
  • hcf: Halts and catches fire, stopping the program immediately. It takes only integer arguments.

An argument is either an integer immediate or a variable reference, which starts with $. Variable names consist of at most 20 lowercase English letters (a-z) or underscores (_).

There are four predefined variables:

  • $acc: Contains the result of the last add or sub operation.
  • $pc: Contains the address of the operation currently being processed.
  • $cmp: Used in comparison by the jeq operation.
  • $out: Is output when the program finishes.

Any other variable is defined as soon as a value is moved into it.

Assume that the input program is syntactically correct. Integers never overflow or underflow.

Output

The program ends when the value of $pc goes outside the bounds of the program, or when an hcf instruction is reached.

If the program ends because $pc goes out of bounds, print one line containing the value of $out at the end of the program.

If the program ends at an hcf instruction, print the following:

  • One line containing the hcf instruction that was called.
  • One line containing the value of $acc.
  • One line containing the value of $cmp.
  • One line containing the value of $out.

Examples2

  1. Example 1

    Input
    6
    mov 3 $cmp
    jeq 3 3
    jeq $cmp 5
    mov 42 $out
    jeq $cmp 7
    mov 50 $out
    
    Expected output
    42
    
  2. Example 2

    Input
    4
    mov 3 $cmp
    mov 2 $out
    add $cmp $out
    hcf 0 0
    
    Expected output
    hcf 0 0
    5
    3
    2