Published
Edited
Dec 11, 2019
1 fork
Insert cell
Insert cell
grid = {
let position = [0, 0];
let direction = [0, 1];
const grid = { '0,0': 1 };
const output = [];
const result = run(
input,
(function*() {
while (true) {
// console.log(grid[position]);
yield grid[position] || 0;
}
})(),
src => {
output.push(src);
if (output.length % 2 === 1) {
// color
grid[position] = src;
} else {
// direction
direction = rotate(direction, src);
position = [position[0] + direction[0], position[1] + direction[1]];
// console.log(position);
}
}
);

let value = result.next();
while (!value.done) {
// yield grid;
value = result.next();
}
yield grid;
}
Insert cell
part1 = Object.keys(grid).length
Insert cell
{
const w = width,
h = width / 4;
const svg = d3.select(
html`<svg width=${w} height=${h} viewBox="${-w / 2} ${-h /
2} ${w} ${h}" style="background-color: lightgray">`
);
svg
.selectAll('circle')
.data(Object.entries(grid))
.enter()
.append('circle')
.attr('cx', d => d[0].split(',')[0] * 5)
.attr('cy', d => -d[0].split(',')[1] * 5)
.attr('r', 2)
.attr('fill', d => (d[1] ? 'white' : 'black'));
return svg.node();
}
Insert cell
wrong = [1060, 2008, 2007]
Insert cell
rotate = (direction, change) => {
const [x, y] = direction;
const t = change ? -Math.PI / 2 : Math.PI / 2;
const sin = Math.sin(t);
const cos = Math.cos(t);
return [+(cos * x - sin * y).toFixed(8), +(sin * x + cos * y).toFixed(8)];
}
Insert cell
function* run(memory, input, output = () => {}) {
memory = memory.split(',').map(BigInt);
memory.input = input;
memory.output = output;
memory.ip = 0;
memory.base = 0;
let opcode;
while ((opcode = memory[memory.ip]) != 99) {
if (opcode === "1207") debugger;
const [p, o = '', ...modes] = String(opcode)
.split('')
.map(Number)
.reverse();
opcode = Number([o, p].join(''));
// console.log({ ip: memory.ip, opcode });
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) => {
switch (modes[i]) {
case 2:
return [
memory[memory.base + Number(p)] || 0,
memory.base + Number(p)
];
case 1:
return [p, p];
case 0:
default:
return [memory[p] || 0, p];
}
})
);
memory.ip =
typeof retval !== "undefined" ? parseInt(retval) : memory.ip + length;
} else {
return {
error: "illegal opcode",
opcode,
ip: memory.ip,
output: memory.output,
memory
};
}
yield memory;
}
yield memory;
}
Insert cell
instructions = ({
1([a], [b], [, dest]) {
// add
this[dest] = BigInt(a) + BigInt(b);
},
2([a], [b], [, dest]) {
// mul
this[dest] = BigInt(a) * BigInt(b);
},
3([, dest]) {
// input
this[dest] = this.input.next().value;
},
4([src]) {
// output
this.output(src);
},
5([test], [value]) {
// jump-if-true
if (typeof test === 'undefined') debugger;
return test ? value : undefined;
},
6([test], [value]) {
// jump-if-false
if (typeof test === 'undefined') debugger;
return !test ? value : undefined;
},
7([a], [b], [, dest]) {
// lt
this[dest] = +(a < b);
},
8([a], [b], [, dest]) {
// eq
this[dest] = +(a == b);
},
9([a]) {
this.base += Number(a);
}
})
Insert cell
// run(`3,3,1105,-1,9,1101,0,0,12,4,12,99,1`, 0)
Insert cell
// run(`1002,4,3,4,33`)
Insert cell
input = `3,8,1005,8,339,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,29,2,1108,11,10,1,1,20,10,2,107,6,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,62,1006,0,29,1006,0,12,1,1101,5,10,1,2,20,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,99,1006,0,30,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,124,1006,0,60,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,149,2,1007,2,10,1,1105,10,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,178,1,1108,15,10,1,1101,5,10,1,109,8,10,1006,0,20,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,215,1006,0,61,1006,0,16,2,1105,15,10,1006,0,50,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,250,1,1003,10,10,1,9,19,10,2,1004,6,10,2,1106,2,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,289,1,1103,13,10,2,105,17,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,318,101,1,9,9,1007,9,1086,10,1005,10,15,99,109,661,104,0,104,1,21101,0,825599304340,1,21101,356,0,0,1106,0,460,21101,0,937108545948,1,21102,1,367,0,1106,0,460,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,1,21628980315,1,21101,0,414,0,1105,1,460,21101,0,3316673539,1,21101,425,0,0,1106,0,460,3,10,104,0,104,0,3,10,104,0,104,0,21102,988753428840,1,1,21102,1,448,0,1106,0,460,21102,825544569700,1,1,21102,459,1,0,1106,0,460,99,109,2,21202,-1,1,1,21102,1,40,2,21102,491,1,3,21102,481,1,0,1105,1,524,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,486,487,502,4,0,1001,486,1,486,108,4,486,10,1006,10,518,1101,0,0,486,109,-2,2105,1,0,0,109,4,2102,1,-1,523,1207,-3,0,10,1006,10,541,21102,0,1,-3,21201,-3,0,1,22102,1,-2,2,21102,1,1,3,21102,560,1,0,1106,0,565,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,588,2207,-4,-2,10,1006,10,588,22101,0,-4,-4,1105,1,656,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21102,1,607,0,1106,0,565,22102,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,626,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,648,21202,-1,1,1,21101,0,648,0,105,1,523,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0`
Insert cell
dump = memory => html`<div style="display: grid; grid-template-columns: repeat(24, 1fr); font-size: x-small; justify-items: end;">
${memory
.filter(d => d)
.map(
(d, i) =>
html`<div style="${
i === memory.ip ? 'font-weight: bold' : ''
}">${d}</div>`
)}
</div>`
Insert cell
d3 = require('d3-selection@1')
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