Skip to content

Commit 003ab05

Browse files
committed
Doesn't work
1 parent a4a37c0 commit 003ab05

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

2019/19/intcode-computer.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class Computer {
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+
144147
this.halted = false;
145148
}
146149

@@ -171,13 +174,25 @@ class Computer {
171174
// "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"
172175
let op = this.OPS[temp_op.substr(-2, 2)];
173176

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

176-
let modes = [];
183+
let modes = this.sharedModes;
177184

178185
// "Parameter modes are single digits, one per parameter, read **right-to-left** from the opcode"
179186
for (let i = op.params - 1; i >= 0; i--) {
180-
modes.push(full_op[i]);
187+
// [0,1,2,3,4,5]
188+
// ^ ops.params = 6
189+
// 5 -> 0 # |(5 - 6 + 1)| = | 0| = 0
190+
// 4 -> 1 # |(4 - 6 + 1)| = |-1| = 1
191+
// 3 -> 2 # |(3 - 6 + 1)| = |-2| = 2
192+
// 2 -> 3 # |(2 - 6 + 1)| = |-3| = 3
193+
// 1 -> 4 # |(1 - 6 + 1)| = |-4| = 4
194+
// 0 -> 5 # |(0 - 6 + 1)| = |-5| = 5
195+
modes[Math.abs(i - op.params + 1)] = full_op[i];
181196
}
182197

183198
let rtn_obj = {
@@ -189,10 +204,10 @@ class Computer {
189204
return rtn_obj;
190205
}
191206

192-
runOp({ modes, fn, jumps, write }) {
207+
runOp({ modes, params, fn, jumps, write }) {
193208
this.pointer++;
194209
let values = [];
195-
for (let i = 0; i < modes.length; i++) {
210+
for (let i = 0; i < params; i++) {
196211
let mode = modes[i];
197212
let value = this.memory[this.pointer + i];
198213

@@ -262,7 +277,7 @@ class Computer {
262277
* - I am running an op that does _not_ write to memory
263278
* - Or if I am not at the last parameter in the op
264279
*/
265-
const can_switch_to_position = !write || i < modes.length - 1;
280+
const can_switch_to_position = !write || i < params - 1;
266281

267282
if (can_switch_to_position && mode === POSITION_MODE) {
268283
value = this.memory[value];

0 commit comments

Comments
 (0)