Published
Edited
Dec 11, 2019
1 star
Insert cell
Insert cell
permutations = combinatorics.permutation([5, 6, 7, 8, 9]).toArray()
Insert cell
async function* feedback(program, sequence) {
console.log({ sequence });
async function* inputs(phase, rest) {
yield Promise.resolve(phase);
yield* rest;
}
let last;
const loop = (async function*() {
console.log("loop", 0);
yield 0;
while (true) {
console.log("loop...", last.value);
yield last.value;
}
})();
const outputs = [run(program, inputs(sequence[0], loop))];
outputs[1] = run(program, inputs(sequence[1], outputs[0]));
outputs[2] = run(program, inputs(sequence[2], outputs[1]));
outputs[3] = run(program, inputs(sequence[3], outputs[2]));
outputs[4] = run(program, inputs(sequence[4], outputs[3]));

last = await outputs[4].next();
while (!last.done) {
yield last.value;
last = await outputs[4].next();
}
}
Insert cell
part2 = (await Promise.all(
permutations.map(p => last(feedback(input, p)))
)).sort((a, b) => b - a)[0]
Insert cell
async function last(generator) {
let i = await generator.next(),
previous;
while (!i.done) {
previous = i;
i = await generator.next();
}
return previous.value;
}
Insert cell
async function* run(program, input) {
const memory = program.split(',').map(Number);
memory.input = input;
memory.ip = 0;

yield* (async function* loop() {
// console.log('ip', memory.ip);
let opcode = memory[memory.ip];
if (opcode === 99) return;
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 = await 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
};
}
if (opcode === 4) {
// output
yield memory.output[memory.output.length - 1];
}

yield* loop();
})();
}
Insert cell
instructions = ({
async 1([a], [b], [, dest]) {
// add
this[dest] = a + b;
},
async 2([a], [b], [, dest]) {
// mul
this[dest] = a * b;
},
async 3([, dest]) {
// input
this[dest] = (await this.input.next()).value;
console.log('input', this[dest]);
},
async 4([src]) {
// output
(this.output = this.output || []).push(src);
},
async 5([test], [value]) {
// jump-if-true
return test ? value : undefined;
},
async 6([test], [value]) {
// jump-if-false
return !test ? value : undefined;
},
async 7([a], [b], [, dest]) {
// lt
this[dest] = +(a < b);
},
async 8([a], [b], [, dest]) {
// eq
this[dest] = +(a === b);
}
})
Insert cell
input = `3,8,1001,8,10,8,105,1,0,0,21,30,39,64,81,102,183,264,345,426,99999,3,9,1001,9,2,9,4,9,99,3,9,1002,9,4,9,4,9,99,3,9,1002,9,5,9,101,2,9,9,102,3,9,9,1001,9,2,9,1002,9,2,9,4,9,99,3,9,1002,9,3,9,1001,9,5,9,1002,9,3,9,4,9,99,3,9,102,4,9,9,1001,9,3,9,102,4,9,9,1001,9,5,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,99`
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
combinatorics = require('js-combinatorics')
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