Published
Edited
Dec 16, 2018
Insert cell
Insert cell
Insert cell
calculate = new Object({
'addr': (r, a, b, c) => r[c] = r[a] + r[b],
'addi': (r, a, b, c) => r[c] = r[a] + b,
'mulr': (r, a, b, c) => r[c] = r[a] * r[b],
'muli': (r, a, b, c) => r[c] = r[a] * b,
'banr': (r, a, b, c) => r[c] = r[a] & r[b],
'bani': (r, a, b, c) => r[c] = r[a] & b,
'borr': (r, a, b, c) => r[c] = r[a] | r[b],
'bori': (r, a, b, c) => r[c] = r[a] | b,
'setr': (r, a, b, c) => r[c] = r[a],
'seti': (r, a, b, c) => r[c] = a,
'gtir': (r, a, b, c) => r[c] = a > r[b] ? 1 : 0,
'gtri': (r, a, b, c) => r[c] = r[a] > b ? 1 : 0,
'gtrr': (r, a, b, c) => r[c] = r[a] > r[b] ? 1 : 0,
'eqir': (r, a, b, c) => r[c] = a === r[b] ? 1 : 0,
'eqri': (r, a, b, c) => r[c] = r[a] === b ? 1 : 0,
'eqrr': (r, a, b, c) => r[c] = r[a] === r[b] ? 1 : 0
})
Insert cell
part1 = {
let out = 0;

for (let i = 0; i < INPUT1.length; i += 3) {
const before = INPUT1[i];
const [code, a, b, c] = INPUT1[i+1];
const after = INPUT1[i+2];

// For a sample, find all operation types that correctly transform the 'before' registers
let operationMatches = new Set();
for (const type of Object.keys(calculate)) {
let r = [...before];
calculate[type](r, a, b, c) // r gets mutated
if (r.every((item, index) => item === after[index])) {
operationMatches.add(type);
}
}

// If 3 or more different operations work, increment our count
if (operationMatches.size >= 3) {
out++;
}
}
return out;
}
Insert cell
Insert cell
function getCandidates () {
let out = {}
for (const type of Object.keys(calculate)) {
out[type] = new Set();
}

for (let i = 0; i < INPUT1.length; i += 3) {
const before = INPUT1[i];
const [code, a, b, c] = INPUT1[i+1];
const after = INPUT1[i+2];

// For a sample, find all operation types that correctly transform the 'before' registers
let operationMatches = new Set();
for (const type of Object.keys(calculate)) {
let r = [...before];
calculate[type](r, a, b, c)
if (r.every((item, index) => item === after[index])) {
out[type].add(code);
}
}
}
return out;
}
Insert cell
getCandidates()
Insert cell
codeToOperation = {
let out = {};
let toIdentify = new Set(Object.keys(calculate));
let candidates = getCandidates();
while (toIdentify.size) {
for (let [type, codes] of Object.entries(candidates)) {
// Find operation types associated with only one code
if (codes.size === 1) {
let code = codes.values().next().value;
out[code] = type;
delete candidates[type];
toIdentify.delete(type);
// Remove that code as a candidate from other operation types
for (let type of Object.keys(candidates)) {
candidates[type].delete(code);
}
// Continue until every code is assigned
}
}
}
return out;
}
Insert cell
part2 = {
let r = [0, 0, 0, 0];
for (const [code, a, b, c] of INPUT2) {
calculate[codeToOperation[code]](r, a, b, c);
}
return r[0];
}
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