Skip to content

Commit 8000e39

Browse files
committed
Also doesnt work
1 parent 003ab05 commit 8000e39

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

2019/19/intcode-computer.js

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Computer {
4141
[ADD]: {
4242
name: ADD,
4343
realName: 'ADD',
44-
params: 3,
44+
params: Array(3).fill('0'),
4545
fn: (a, b, c) => {
4646
this.memory[c] = a + b;
4747
},
@@ -51,7 +51,7 @@ class Computer {
5151
[MUL]: {
5252
name: MUL,
5353
realName: 'MUL',
54-
params: 3,
54+
params: Array(3).fill('0'),
5555
fn: (a, b, c) => {
5656
this.memory[c] = a * b;
5757
},
@@ -61,7 +61,7 @@ class Computer {
6161
[INP]: {
6262
name: INP,
6363
realName: 'INP',
64-
params: 1,
64+
params: Array(1).fill('0'),
6565
fn: a => {
6666
this.memory[a] = this.inputs.shift();
6767
if (this.replenish_input !== undefined) {
@@ -74,28 +74,28 @@ class Computer {
7474
[OUT]: {
7575
name: OUT,
7676
realName: 'OUT',
77-
params: 1,
77+
params: Array(1).fill('0'),
7878
fn: a => this.output(a),
7979
},
8080

8181
[ARB]: {
8282
name: ARB,
8383
realName: 'ARB',
84-
params: 1,
84+
params: Array(1).fill('0'),
8585
fn: a => (this.relative_base += a),
8686
},
8787

8888
[STP]: {
8989
name: STP,
9090
realName: 'STP',
91-
params: 0,
91+
params: Array(0).fill('0'),
9292
fn: () => (this.halted = true),
9393
},
9494

9595
[JIT]: {
9696
name: JIT,
9797
realName: 'JIT',
98-
params: 2,
98+
params: Array(2).fill('0'),
9999
fn: (a, b) => {
100100
if (a) {
101101
this.pointer = b;
@@ -109,7 +109,7 @@ class Computer {
109109
[JIF]: {
110110
name: JIF,
111111
realName: 'JIF',
112-
params: 2,
112+
params: Array(2).fill('0'),
113113
fn: (a, b) => {
114114
if (!a) {
115115
this.pointer = b;
@@ -123,7 +123,7 @@ class Computer {
123123
[LTH]: {
124124
name: LTH,
125125
realName: 'LTH',
126-
params: 3,
126+
params: Array(3).fill('0'),
127127
fn: (a, b, c) => {
128128
this.memory[c] = a < b ? 1 : 0;
129129
},
@@ -133,17 +133,14 @@ class Computer {
133133
[EQU]: {
134134
name: EQU,
135135
realName: 'EQU',
136-
params: 3,
136+
params: Array(3).fill('0'),
137137
fn: (a, b, c) => {
138138
this.memory[c] = a === b ? 1 : 0;
139139
},
140140
write: true,
141141
},
142142
};
143143

144-
this.maxParams = Math.max(...Object.values(this.OPS).map(v => v.params));
145-
this.sharedModes = Array(this.maxParams).fill('0');
146-
147144
this.halted = false;
148145
}
149146

@@ -174,16 +171,10 @@ class Computer {
174171
// "The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction"
175172
let op = this.OPS[temp_op.substr(-2, 2)];
176173

177-
if (!op) {
178-
debugger;
179-
}
180-
181174
let full_op = temp_op.padStart(op.params + 2, '0');
182175

183-
let modes = this.sharedModes;
184-
185176
// "Parameter modes are single digits, one per parameter, read **right-to-left** from the opcode"
186-
for (let i = op.params - 1; i >= 0; i--) {
177+
for (let i = op.params.length - 1; i >= 0; i--) {
187178
// [0,1,2,3,4,5]
188179
// ^ ops.params = 6
189180
// 5 -> 0 # |(5 - 6 + 1)| = | 0| = 0
@@ -192,23 +183,19 @@ class Computer {
192183
// 2 -> 3 # |(2 - 6 + 1)| = |-3| = 3
193184
// 1 -> 4 # |(1 - 6 + 1)| = |-4| = 4
194185
// 0 -> 5 # |(0 - 6 + 1)| = |-5| = 5
195-
modes[Math.abs(i - op.params + 1)] = full_op[i];
186+
op.params[Math.abs(i - op.params + 1)] = full_op[i];
196187
}
197188

198-
let rtn_obj = {
199-
...op,
200-
modes,
201-
};
202189
let end = new Date();
203190
this.parseOpTime += (end - start);
204-
return rtn_obj;
191+
return op;
205192
}
206193

207-
runOp({ modes, params, fn, jumps, write }) {
194+
runOp({ params, fn, jumps, write }) {
208195
this.pointer++;
209196
let values = [];
210-
for (let i = 0; i < params; i++) {
211-
let mode = modes[i];
197+
for (let i = 0; i < params.length; i++) {
198+
let mode = params[i];
212199
let value = this.memory[this.pointer + i];
213200

214201
// Values can overflow existing memory. If so, value should be 0
@@ -277,7 +264,7 @@ class Computer {
277264
* - I am running an op that does _not_ write to memory
278265
* - Or if I am not at the last parameter in the op
279266
*/
280-
const can_switch_to_position = !write || i < params - 1;
267+
const can_switch_to_position = !write || i < params.length - 1;
281268

282269
if (can_switch_to_position && mode === POSITION_MODE) {
283270
value = this.memory[value];
@@ -308,7 +295,7 @@ class Computer {
308295
let result = fn(...values);
309296

310297
if (!jumps || (jumps && !result)) {
311-
this.pointer += modes.length;
298+
this.pointer += params.length;
312299
}
313300
}
314301

0 commit comments

Comments
 (0)