-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (41 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
export const executeCommands = (commands = []) => {
let registers = [0, 0, 0, 0, 0, 0, 0, 0];
let pointer = 0;
let actions = {
MOV: (value, memoryPointer) => {
memoryPointer = +memoryPointer.replace(/V\d/, '');
if (isNaN(+value)) {
value = +value.replace(/V\d/, '');
registers[memoryPointer] = registers[value];
} else {
registers[memoryPointer] = +value;
}
++pointer;
},
DEC: memoryPointer => {
memoryPointer = +memoryPointer.replace(/V\d/, '');
registers[memoryPointer] = (((registers[memoryPointer] - 1) % 256) + 256) % 256;
++pointer;
},
INC: memoryPointer => {
memoryPointer = +memoryPointer.replace(/V\d/, '');
registers[memoryPointer] = (registers[memoryPointer] + 1) % 256;
++pointer;
},
ADD: (base, add) => {
base = +base.replace(/V\d/, '');
add = +add.replace(/V\d/, '');
registers[base] = (registers[base] + registers[add]) % 256;
++pointer;
},
JMP: index => {
pointer = registers[0] ? +index : pointer + 1;
},
};
while (pointer < commands.length) {
let [command, ...args] = commands[pointer].split(' ');
args = args[0].split(',');
actions[command](...args);
}
return registers;
};