Published
Edited
Dec 9, 2019
2 forks
Insert cell
Insert cell
part2 = run(input, 5)
Insert cell
dump(part2)
Insert cell
part2.output
Insert cell
part1 = run(input, 1)
Insert cell
part1.output
Insert cell
dump(part1)
Insert cell
function* run(memory, input) {
memory = memory.split(',').map(Number);
memory.input = input;
memory.ip = 0;
let opcode;
while ((opcode = memory[memory.ip]) !== 99) {
const [p, o = '', ...modes] = String(opcode)
.split('')
.map(Number)
.reverse();
opcode = Number([o, p].join(''));
const instruction = instructions[opcode];
if (instruction) {
const length = instruction.length + 1;
const retval = instruction.apply(
memory,
memory
.slice(memory.ip + 1, memory.ip + length)
.map((p, i) => [modes[i] ? p : memory[p], p])
);
memory.ip = typeof retval !== "undefined" ? retval : memory.ip + length;
} else {
return {
error: "illegal opcode",
opcode,
ip: memory.ip,
output: memory.output,
memory
};
}
yield memory;
}
return memory;
}
Insert cell
instructions = ({
1([a], [b], [, dest]) {
// add
this[dest] = a + b;
},
2([a], [b], [, dest]) {
// mul
this[dest] = a * b;
},
3([, dest]) {
// input
this[dest] = this.input;
},
4([src]) {
// output
(this.output = this.output || []).push(src);
},
5([test], [value]) {
// jump-if-true
return test ? value : undefined;
},
6([test], [value]) {
// jump-if-false
return !test ? value : undefined;
},
7([a], [b], [, dest]) {
// lt
this[dest] = +(a < b);
},
8([a], [b], [, dest]) {
// eq
this[dest] = +(a === b);
}
})
Insert cell
run(`3,3,1105,-1,9,1101,0,0,12,4,12,99,1`, 2)
Insert cell
run(`1002,4,3,4,33`)
Insert cell
input = `3,225,1,225,6,6,1100,1,238,225,104,0,1101,81,30,225,1102,9,63,225,1001,92,45,224,101,-83,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,1102,41,38,225,1002,165,73,224,101,-2920,224,224,4,224,102,8,223,223,101,4,224,224,1,223,224,223,1101,18,14,224,1001,224,-32,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1101,67,38,225,1102,54,62,224,1001,224,-3348,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,1,161,169,224,101,-62,224,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,2,14,18,224,1001,224,-1890,224,4,224,1002,223,8,223,101,3,224,224,1,223,224,223,1101,20,25,225,1102,40,11,225,1102,42,58,225,101,76,217,224,101,-153,224,224,4,224,102,8,223,223,1001,224,5,224,1,224,223,223,102,11,43,224,1001,224,-451,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,1102,77,23,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,226,677,224,1002,223,2,223,1006,224,329,1001,223,1,223,7,226,226,224,102,2,223,223,1006,224,344,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,359,101,1,223,223,1107,226,677,224,1002,223,2,223,1005,224,374,101,1,223,223,1008,677,226,224,1002,223,2,223,1005,224,389,101,1,223,223,1007,677,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,419,1001,223,1,223,108,677,226,224,102,2,223,223,1006,224,434,1001,223,1,223,7,226,677,224,102,2,223,223,1005,224,449,1001,223,1,223,107,226,226,224,102,2,223,223,1006,224,464,101,1,223,223,107,677,226,224,102,2,223,223,1006,224,479,101,1,223,223,1007,677,677,224,1002,223,2,223,1006,224,494,1001,223,1,223,1008,226,226,224,1002,223,2,223,1006,224,509,101,1,223,223,7,677,226,224,1002,223,2,223,1006,224,524,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,539,101,1,223,223,8,677,226,224,1002,223,2,223,1006,224,554,101,1,223,223,1008,677,677,224,102,2,223,223,1006,224,569,101,1,223,223,1108,677,226,224,102,2,223,223,1005,224,584,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,599,1001,223,1,223,1108,677,677,224,1002,223,2,223,1006,224,614,1001,223,1,223,1107,677,677,224,1002,223,2,223,1005,224,629,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,644,101,1,223,223,8,226,226,224,1002,223,2,223,1005,224,659,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,674,101,1,223,223,4,223,99,226`
Insert cell
dump = memory => html`<div style="display: grid; grid-template-columns: repeat(24, 1fr); font-size: x-small; justify-items: end;">
${memory.map(
(d, i) =>
html`<div style="${
i === memory.ip ? 'font-weight: bold' : ''
}">${d}</div>`
)}
</div>`
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