Published
Edited
Nov 13, 2019
1 fork
Insert cell
Insert cell
part2 = {
const registers = [ 0, 0, 0, 0 ]
for (const line of program)
opcodes[line[0]](...line.slice(1), registers)
return registers
}
Insert cell
Insert cell
opcodes = {
const opcodes = {}
let unmatchedOperations = operations
let n = 0
while (unmatchedOperations.length && n++ < 10) {
for (const sample of samples) {
const op = sample.instruction[0]
if (op in opcodes)
continue

const opMatches = unmatchedOperations.filter(op => matches(op, sample))
if (opMatches.length === 1)
unmatchedOperations = _.without(unmatchedOperations, opcodes[op] = opMatches[0])
}
}
return opcodes
}
Insert cell
part1 = {
const opcodeMatches = samples.map(sample => {
return operations.filter(op => matches(op, sample))
})
return opcodeMatches.filter(matches => matches.length >= 3).length
}
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
function matches(op, { before, instruction, after }) {
const registers = [...before]
op(...instruction.slice(1), registers)
for (let i = 0; i < 4; i++)
if (registers[i] !== after[i])
return false
return true
}
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