Published
Edited
Dec 5, 2019
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/**
* HoF that returns a function that performs an operation given by `fn`. The returned function takes an
* array `mem` (memory) and integer `ip` (instruction pointer). It returns a 2-tuple of the operation's
* optional output and the next position of the instruction pointer.
* @param {function(Array<number>, ...Array<number>)} fn
* @return {function(Array<number>, number): Array<(number|null)>}
*/
function op(fn) {
const arity = fn.length - 1;
const fn2 = (mem, ip) => {
const nextIp = ip + arity + 1;

let modes = Math.floor(mem[ip] / 100);
const params = mem.slice(ip + 1, nextIp).map(p => {
const mode = modes % 10;
modes = Math.floor(modes / 10);
return [getParamValue(mem, p, mode), p];
});
const [output = null, setIp = null] = fn(mem, ...params);
return [setIp === null ? nextIp : setIp, output];
};
fn2.arity = fn.length - 1;
return fn2;
}
Insert cell
function getParamValue(mem, param, mode) {
switch (mode) {
case ParamMode.POSITION:
return mem[param];
case ParamMode.IMMEDIATE:
return param;
default:
throw new Error(`Invalid mode '${mode}'`);
}
}
Insert cell
Ops1 = programInput => ({
[Opcode.ADD]: op((mem, [v1], [v2], [, p3]) => ((mem[p3] = v1 + v2), [])),
[Opcode.MULT]: op((mem, [v1], [v2], [, p3]) => ((mem[p3] = v1 * v2), [])),
// takes a single integer as input and saves it to the position given by its only parameter
[Opcode.GET]: op((mem, [, p1]) => ((mem[p1] = parseInt(programInput)), [])),
// outputs the value of its only parameter
[Opcode.PUT]: op((_, [v1]) => [v1]),
[Opcode.HALT]: op(_ => [null, -1])
})
Insert cell
function getPerformInstruction(ops) {
return (prog, ip) => {
const opcode = prog[ip] % 100;
if (opcode in ops) {
return ops[opcode](prog, ip);
}
throw new Error(`Invalid opcode '${opcode}' at ${ip}`);
};
}
Insert cell
function* evaluateIntcode(prog, ops) {
let ip = 0;
let output;

const performInstruction = getPerformInstruction(ops);

while (ip !== -1 && ip < prog.length) {
[ip, output] = performInstruction(prog, ip);
if (output !== null) {
yield output;
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Ops2 = programInput =>
Object.assign(Ops1(programInput), {
// If the first parameter is non-zero, it sets the instruction pointer to the value from the second
// parameter. Otherwise, it does nothing.
[Opcode2.JUMP_IF_TRUE]: op((_, [v1], [v2]) =>
v1 === 0 ? [] : [null, v2]
),
// If the first parameter is zero, it sets the instruction pointer to the value from the second
// parameter. Otherwise, it does nothing.
[Opcode2.JUMP_IF_FALSE]: op((_, [v1], [v2]) =>
v1 === 0 ? [null, v2] : []
),
// If the first parameter is less than the second parameter, it stores 1 in the position given by the
// third parameter. Otherwise, it stores 0.
[Opcode2.LESS_THAN]: op(
(mem, [v1], [v2], [, p3]) => ((mem[p3] = +(v1 < v2)), [])
),
// If the first parameter is equal to the second parameter, it stores 1 in the position given by the
// third parameter. Otherwise, it stores 0.
[Opcode2.EQUALS]: op(
(mem, [v1], [v2], [, p3]) => ((mem[p3] = +(v1 === v2)), [])
)
})
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more