Skip to content

Commit a4a37c0

Browse files
committed
Testing parseOp
1 parent cc30e1f commit a4a37c0

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

2019/19/intcode-computer.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Computer {
3535
this.replenish_input = replenish_input;
3636
this.outputs = [];
3737

38+
this.parseOpTime = 0;
39+
3840
this.OPS = {
3941
[ADD]: {
4042
name: ADD,
@@ -163,6 +165,7 @@ class Computer {
163165
}
164166

165167
parseOp() {
168+
let start = new Date();
166169
let temp_op = String(this.memory[this.pointer]).padStart(2, '0');
167170

168171
// "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"
@@ -177,10 +180,13 @@ class Computer {
177180
modes.push(full_op[i]);
178181
}
179182

180-
return {
183+
let rtn_obj = {
181184
...op,
182185
modes,
183186
};
187+
let end = new Date();
188+
this.parseOpTime += (end - start);
189+
return rtn_obj;
184190
}
185191

186192
runOp({ modes, fn, jumps, write }) {
@@ -299,6 +305,11 @@ class Computer {
299305
get _() {
300306
return this.memory.slice(Math.max(0, this.pointer - 1), this.pointer + 8);
301307
}
308+
309+
logParseOpDuration() {
310+
const million = BigInt(1e6);
311+
console.log(`parseOp took ${parseOpTime / million} ms`);
312+
}
302313
}
303314

304315
module.exports = {

2019/19/part-two.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ const { TractorBeam } = require('./tractor-beam');
44
let tractor_beam = new TractorBeam(input);
55
// console.log(tractor_beam.partTwo());
66
console.log(tractor_beam.partTwoOptimized());
7+
console.log(`parseOp took ${tractor_beam.parseOpTime} ms`);

2019/19/tractor-beam.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class TractorBeam {
55
constructor(memory, options = {}) {
66
this.memory = memory.slice(0);
77
this.grid = new InfiniteGrid({ string_map: { 1: '#', 0: '.' } });
8+
9+
this.parseOpTime = 0;
810
}
911

1012
partOne() {
@@ -192,22 +194,14 @@ class TractorBeam {
192194
// First, move down until we hit 0
193195
do {
194196
bottom_edge.y++;
195-
let computer = new Computer({
196-
memory: this.memory,
197-
inputs: [bottom_edge.x, bottom_edge.y],
198-
});
199-
[output] = computer.run();
197+
output = this.computeAt(bottom_edge.x, bottom_edge.y);
200198
this.grid.set(bottom_edge.x, bottom_edge.y, output);
201199
} while (output === 1);
202200

203201
// Then, move inward until we hit a `1`
204202
do {
205203
bottom_edge.x++;
206-
let computer = new Computer({
207-
memory: this.memory,
208-
inputs: [bottom_edge.x, bottom_edge.y],
209-
});
210-
[output] = computer.run();
204+
output = this.computeAt(bottom_edge.x, bottom_edge.y);
211205
this.grid.set(bottom_edge.x, bottom_edge.y, output);
212206
} while (output === 0);
213207
}
@@ -220,11 +214,7 @@ class TractorBeam {
220214
do {
221215
// Move right until we hit a `0`
222216
top_edge.x++;
223-
let computer = new Computer({
224-
memory: this.memory,
225-
inputs: [top_edge.x, top_edge.y],
226-
});
227-
[output] = computer.run();
217+
output = this.computeAt(top_edge.x, top_edge.y);
228218
this.grid.set(top_edge.x, top_edge.y, output);
229219
} while (output === 1);
230220
} while (allow_for_bottom_jumps && top_edge.y !== bottom_edge.y);
@@ -242,6 +232,8 @@ class TractorBeam {
242232
// The computer halts after every output, so we create a new one each time
243233
let computer = new Computer({ memory: this.memory, inputs: [x, y] });
244234
let [output] = computer.run();
235+
this.parseOpTime += computer.parseOpTime;
236+
245237
return output;
246238
}
247239
}

0 commit comments

Comments
 (0)