-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcomputer.js
161 lines (134 loc) · 2.76 KB
/
computer.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const ADD = '01'; // Add
const MUL = '02'; // Multiply
const INP = '03'; // Input
const OUT = '04'; // Output
const JIT = '05'; // Jump-if-true
const JIF = '06'; // Jump-if-false
const LTH = '07'; // Less Than
const EQU = '08'; // Equals
const STP = '99'; // Stop
const POSITION_MODE = '0';
const IMMEDIATE_MODE = '1';
class Computer {
constructor(input, input_value = 1) {
this.original_input = input.slice(0);
this.input = input.slice(0);
this.pointer = 0;
this.input_value = input_value;
this.output_value = null;
this.OPS = {
[ADD]: {
name: ADD,
params: 3,
fn: (a, b, c) => (this.input[c] = a + b),
write: true,
},
[MUL]: {
name: MUL,
params: 3,
fn: (a, b, c) => (this.input[c] = a * b),
write: true,
},
[INP]: {
name: INP,
params: 1,
fn: a => (this.input[a] = this.input_value),
write: true,
},
[OUT]: {
name: OUT,
params: 1,
fn: a => this.output(a),
},
[STP]: {
name: STP,
params: 0,
fn: (...a) => console.log('STOP', a),
},
[JIT]: {
name: JIT,
params: 2,
fn: (a, b) => {
if (a) {
this.pointer = b;
return true;
}
return false;
},
jumps: true,
},
[JIF]: {
name: JIF,
params: 2,
fn: (a, b) => {
if (!a) {
this.pointer = b;
return true;
}
return false;
},
jumps: true,
},
[LTH]: {
name: LTH,
params: 3,
fn: (a, b, c) => (this.input[c] = a < b ? 1 : 0),
write: true,
},
[EQU]: {
name: EQU,
params: 3,
fn: (a, b, c) => (this.input[c] = a === b ? 1 : 0),
write: true,
},
};
}
run() {
let op = this.parseOp();
while (op.name !== STP) {
this.runOp(op);
op = this.parseOp();
}
}
parseOp() {
let temp_op = String(this.input[this.pointer]).padStart(2, '0');
let op = this.OPS[temp_op.substr(-2, 2)];
let full_op = temp_op.padStart(op.params + 2, '0');
let modes = [];
for (let i = op.params - 1; i >= 0; i--) {
modes.push(full_op[i]);
}
return {
...op,
modes,
};
}
runOp({ modes, fn, jumps, write }) {
this.pointer++;
let values = [];
for (let i = 0; i < modes.length; i++) {
let mode = modes[i];
let value = this.input[this.pointer + i];
const can_switch_to_position = !write || i < modes.length - 1;
if (can_switch_to_position && mode === POSITION_MODE) {
value = this.input[value];
}
values.push(value);
}
// If result is `true`, we moved the pointer
let result = fn(...values);
if (!jumps || (jumps && !result)) {
this.pointer += modes.length;
}
}
output(v) {
if (v !== 0) {
console.log(v);
}
}
// For debugging
// get _() {
// return this.input.slice(Math.max(0, this.pointer - 1), this.pointer + 8);
// }
}
module.exports = Computer;