Published
Edited
Dec 4, 2019
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function evaluateIntcode(prog, pos = 0) {
const [opcode, p1, p2, d] = prog.slice(pos, pos + 4);

switch (opcode) {
case 1:
prog[d] = prog[p1] + prog[p2];
break;
case 2:
prog[d] = prog[p1] * prog[p2];
break;
case 99:
return prog;
default:
throw new Error(`Invalid opcode '${opcode}' at ${pos}`);
}

return evaluateIntcode(prog, pos + 4);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function computeGravityAssist(target) {
let i, j;
for (i = 0; i <= 99; i++) {
for (j = 0; j <= 99; j++) {
const output = runIntcodeProgram(input, [[1, i], [2, j]]);
if (output === target) {
return [i, j];
}
}
}
throw new Error(`Target '${target}' was not computable`);
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more