Published
Edited
Nov 13, 2019
Fork of Day 16
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
part1 = {
const registers = [ 0, 0, 0, 0, 0, 0 ]
let ip = 0
function boundOperations(i) {
const bound = {}
for (const op of operations) {
bound[op.name] = (a, b, c, r) => {
r[i] = ip
op(a, b, c, r)
ip = r[i]
}
}
return bound
}
const ops = boundOperations(1)
while (ip in program) {
const line = program[ip]
const cip = ip
ops[line.op](...line.args, registers)
const r = registers
ip++
console.log(`${cip}: x=${r[3]} y=${r[5]} a=${r[0]} b=${r[2]} ${line.op} ${line.args} ${registers}`)
}
return { ip, registers }
}
Insert cell
operations = [
function addr(a, b, c, r) { r[c] = r[a] + r[b] },
function addi(a, b, c, r) { r[c] = r[a] + b },
function mulr(a, b, c, r) { r[c] = r[a] * r[b] },
function muli(a, b, c, r) { r[c] = r[a] * b },
function banr(a, b, c, r) { r[c] = r[a] & r[b] },
function bani(a, b, c, r) { r[c] = r[a] & b },
function borr(a, b, c, r) { r[c] = r[a] | r[b] },
function bori(a, b, c, r) { r[c] = r[a] | b },
function setr(a, b, c, r) { r[c] = r[a] },
function seti(a, b, c, r) { r[c] = a },
function gtir(a, b, c, r) { r[c] = a > r[b] ? 1 : 0 },
function gtri(a, b, c, r) { r[c] = r[a] > b ? 1 : 0 },
function gtrr(a, b, c, r) { r[c] = r[a] > r[b] ? 1 : 0 },
function eqir(a, b, c, r) { r[c] = a === r[b] ? 1 : 0 },
function eqri(a, b, c, r) { r[c] = r[a] === b ? 1 : 0 },
function eqrr(a, b, c, r) { r[c] = r[a] === r[b] ? 1 : 0 },
]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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