-
Notifications
You must be signed in to change notification settings - Fork 17
/
circuit.js
231 lines (187 loc) · 4.26 KB
/
circuit.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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(memory, inputs, id, clone_memory = false) {
// For debugging
this.id = String.fromCharCode('A'.charCodeAt(0) + id);
this.original_memory = clone_memory && memory.slice(0);
this.memory = memory.slice(0);
this.pointer = 0;
this.inputs = Array.isArray(inputs) ? inputs.slice(0) : [inputs];
this.outputs = [];
this.OPS = {
[ADD]: {
name: ADD,
params: 3,
fn: (a, b, c) => (this.memory[c] = a + b),
write: true,
},
[MUL]: {
name: MUL,
params: 3,
fn: (a, b, c) => (this.memory[c] = a * b),
write: true,
},
[INP]: {
name: INP,
params: 1,
fn: a => (this.memory[a] = this.inputs.shift()),
write: true,
},
[OUT]: {
name: OUT,
params: 1,
fn: a => this.output(a),
},
[STP]: {
name: STP,
params: 0,
fn: () => (this.halted = true),
},
[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.memory[c] = a < b ? 1 : 0),
write: true,
},
[EQU]: {
name: EQU,
params: 3,
fn: (a, b, c) => (this.memory[c] = a === b ? 1 : 0),
write: true,
},
};
this.halted = false;
}
run() {
let op = this.parseOp();
while (!this.halted) {
this.runOp(op);
// Pause executing the computer so this output can be given to the next computer
// Additionally, break if we've halted
if (op.name === OUT || this.halted) {
break;
}
op = this.parseOp();
}
return this.outputs;
}
parseOp() {
let temp_op = String(this.memory[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.memory[this.pointer + i];
const can_switch_to_position = !write || i < modes.length - 1;
if (can_switch_to_position && mode === POSITION_MODE) {
value = this.memory[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) {
this.outputs.push(v);
}
// For debugging
get _() {
return this.memory.slice(
Math.max(0, this.pointer - 1),
this.pointer + 8
);
}
}
class Circuit {
constructor(memory, phase_settings, circuit_size = 5) {
this.memory = memory;
this.phase_settings = phase_settings;
this.circuit = Array(circuit_size)
.fill()
.map((c, i) => {
let phase_setting = [phase_settings[i]];
if (i === 0) {
// "The first amplifier's input value is 0"
phase_setting.push(0);
}
return new Computer(memory, phase_setting, i);
});
this.current_computer = 0;
}
run() {
let computer = this.circuit[this.current_computer];
let output, last_output;
while (!computer.halted) {
let new_output = computer.run();
if (computer.halted) {
break;
}
output = new_output;
let next_computer = this.moveToNextComputer();
// `output.pop` removes the value from the computers `this.outputs` array,
// meaning the next computer effectively "consumes" the value
last_output = output.shift();
next_computer.inputs.push(last_output);
computer = next_computer;
}
return last_output;
}
moveToNextComputer() {
this.current_computer++;
this.current_computer %= this.circuit.length;
return this.circuit[this.current_computer];
}
}
module.exports = {
Computer,
Circuit,
};