Interpreter

Time limit1sMemory limit128 MB

Summary
Simulate a 10-register, 1000-word RAM machine that executes encoded 3-digit instructions, and count how many instructions run before the halt.
Level

Medium4 of 10

Topics
Simulation, Implementation, Array, Brute force
Solved
No attempts yet

Problem

A small computer has 10 registers and 1000 words of RAM. Each register and each RAM word stores a 3-digit integer from 0 to 999. Instructions are encoded as 3-digit integers and are stored in RAM. Every register starts at 000, and the initial RAM contents are given on standard input. Execution begins with the instruction at RAM address 0. Every arithmetic result is reduced modulo 1000.

The instruction encodings are:

  • 100 — halt.
  • 2dn — set register d to n.
  • 3dn — add n to register d.
  • 4dn — multiply register d by n.
  • 5ds — set register d to the value of register s.
  • 6ds — add the value of register s to register d.
  • 7ds — multiply register d by the value of register s.
  • 8da — set register d to the RAM word whose address is held in register a.
  • 9sa — store the value of register s into the RAM word whose address is held in register a.
  • 0ds — jump to the address held in register d, unless register s holds 0 (in which case continue with the next instruction).

Here d, s, and a are single digits (0-9) naming a register, and n is a single digit (0-9).

Input

The input consists of up to 1000 three-digit unsigned integers, one per line, giving the contents of consecutive RAM words starting at address 0. Any RAM word not listed is initialized to 000.

Output

Print a single integer: the number of instructions executed, counting up to and including the halt instruction. The program is guaranteed to halt, in at most 100000 instructions.

Examples5

  1. Example 1

    Input
    299
    492
    495
    399
    492
    495
    399
    283
    279
    689
    078
    100
    000
    000
    000
    
    Expected output
    16
    
  2. Example 2

    Input
    100
    
    Expected output
    1
    
  3. Example 3

    Input
    235
    100
    
    Expected output
    2
    
  4. Example 4

    Input
    204
    211
    001
    100
    100
    
    Expected output
    4
    
  5. Example 5

    Input
    203
    002
    100
    
    Expected output
    3