Published
Edited
Dec 23, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
part1 = {
let register = [0, 0, 0, 0, 0, 0];

while (register[IP_REGISTER] < INSTRUCTIONS.length && register[IP_REGISTER] >= 0) {
if (register[IP_REGISTER] === 28) {
return register[5];
}

const [currentInstruction, a, b, c] = INSTRUCTIONS[register[IP_REGISTER]];
calculate[currentInstruction](register, a, b, c);
register[IP_REGISTER]++;
}
}
Insert cell
Insert cell
part2 = {
// Too slow to run in browser without reverse engineering the ElfCode
let B, E;
let prev;
let seen = new Set();
B = 65536;
E = 7571367;

while (true) {
E += B & 255;
E &= 16777215;
E *= 65899;
E &= 16777215;

if (B < 256) {
// Find the last new value of E during instruction 28 before it cycles
// That will be the last hit, so setting r[0] to its value results
// in the most instructions among all possible terminating values.
if (seen.has(E)) {
return prev;
} else {
seen.add(E);
prev = E;
}
B = E | 65536;
E = 7571367;
} else {
B = Math.floor(B / 256);
}
}
}
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