-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
44 lines (39 loc) · 923 Bytes
/
solution.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
/**
* @param {string[]} instructions - The instructions to execute
* @returns {number} The value of the register A
*/
function compile(instructions) {
var pointer = 0;
var registry = {};
while (pointer < instructions.length) {
const instruction = instructions[pointer];
const [action, x, y] = instruction.split(" ");
if (action === "MOV") {
if (x in registry) {
registry[y] = registry[x];
} else {
registry[y] = Number(x);
}
pointer++;
}
if (action === "INC") {
registry[x] = registry[x] || 0;
registry[x]++;
pointer++;
}
if (action === "DEC") {
registry[x] = registry[x] || 0;
registry[x]--;
pointer++;
}
if (action == "JMP") {
let value = registry[x] || 0;
if (value === 0) {
pointer = Number(y);
} else {
pointer++;
}
}
}
return registry["A"];
}