-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint-code-computer.ts
116 lines (103 loc) · 3.2 KB
/
int-code-computer.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
export class IntCodeComputer {
private code: number[];
private defaultInput: number | undefined;
private inputs: number[];
private lastIndex: number;
private relativeBase: number;
public output: number | null;
constructor(code: string, inputs: number[] = [], defaultInput?: number) {
if (code.length === 0) {
throw new Error("Empty code! Test case not defined?");
}
this.code = code.split(",").map(Number);
this.lastIndex = 0;
this.output = null;
this.inputs = inputs;
this.relativeBase = 0;
this.defaultInput = defaultInput;
}
public addInputs(inputs: number[]) {
this.inputs.push(...inputs);
}
public compute(restart: boolean = true): boolean {
this.output = null;
for (let i = restart ? 0 : this.lastIndex; i < this.code.length; i += 1) {
this.lastIndex = i;
const code = this.code[i];
const [a, b, c, ...opcodes] = code.toString().padStart(5, "0").split("").map(Number);
const opcode = +opcodes.join("");
const index1 = c === 0 ? this.code[i + 1] : c === 1 ? i + 1 : this.code[i + 1] + this.relativeBase;
const index2 = b === 0 ? this.code[i + 2] : b === 1 ? i + 2 : this.code[i + 2] + this.relativeBase;
const index3 = a === 0 ? this.code[i + 3] : a === 1 ? i + 3 : this.code[i + 3] + this.relativeBase;
const param1 = this.code[index1];
const param2 = this.code[index2];
// console.log(JSON.stringify({ i, code, opcode, index1, index2, index3, param1, param2, inputs: this.inputs }));
switch (opcode) {
case 1:
this.code[index3] = param1 + param2;
i += 3;
break;
case 2:
this.code[index3] = param1 * param2;
i += 3;
break;
case 3:
let breakAfter = false;
if (this.inputs.length === 0) {
if (this.defaultInput) {
this.inputs.push(this.defaultInput);
breakAfter = true;
} else {
throw new Error(`Unexpected opcode 3 with empty inputs!`);
}
}
this.code[index1] = this.inputs.shift()!;
i += 1;
if (breakAfter) {
this.lastIndex = i + 1;
return false;
}
break;
case 4:
this.output = param1;
i += 1;
this.lastIndex = i + 1;
return false;
case 5:
if (param1 !== 0) {
i = param2 - 1;
} else {
i += 2;
}
break;
case 6:
if (param1 === 0) {
i = param2 - 1;
} else {
i += 2;
}
break;
case 7:
this.code[index3] = param1 < param2 ? 1 : 0;
i += 3;
break;
case 8:
this.code[index3] = param1 === param2 ? 1 : 0;
i += 3;
break;
case 9:
this.relativeBase += param1;
i += 1;
break;
case 99:
return true;
default:
throw new Error(`Unknwon opcode ${JSON.stringify({ opcode, index1, index2, index3, param1, param2, inputs: this.inputs })}`);
}
}
return false;
}
public inputQueue() {
return this.inputs;
}
}