The elf programmers are creating a small magical assembler to control the machines in Santa Claus's workshop.
To assist them, we will implement a simple interpreter that supports the following magical instructions:
MOV x y: Copies the value x (which can be a number or the content of a register) into register yINC x: Increments the content of register x by 1DEC x: Decrements the content of register x by 1JMP x y: If the value in register x is 0, then jumps to the instruction at index y and continues executing the program from there.0 will be used.JMP is absolute and goes to the exact index indicated by y.A. If A did not have a defined value, it returns undefined.const instructions = [
'MOV -1 C', // copies -1 to register 'C',
'INC C', // increments the value of register 'C'
'JMP C 1', // jumps to the instruction at index 1 if 'C' is 0
'MOV C A', // copies register 'C' to register 'A',
'INC A' // increments the value of register 'A'
]
compile(instructions) // -> 2
/**
Step-by-step execution:
0: MOV -1 C -> The register C receives the value -1
1: INC C -> The register C becomes 0
2: JMP C 1 -> C is 0, jumps to the instruction at index 1
1: INC C -> The register C becomes 1
2: JMP C 1 -> C is 1, the instruction is ignored
3: MOV C A -> Copies register C to A. Now A is 1
4: INC A -> The register A becomes 2
*/
Note: Registers that have not been previously initialized are initialized to 0.