Public
Edited
Nov 27, 2022
1 star
Insert cell
Insert cell
Insert cell
function ampOut(input, phase) {
return new Computer([phase, input], puzzleInput).run().out;
}
Insert cell
Insert cell
function amplifiers(phases) {
return phases.slice(1).reduce(ampOut, ampOut(0, phases[0]));
}
Insert cell
Insert cell
function part1() {
return Math.max(...AOC.permutations([0, 1, 2, 3, 4]).map(amplifiers));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function generateSignal(phases) {
const createAmp = (input, phase) =>
new Computer([phase, input], puzzleInput).run();
const amps = AOC.scanl(
phases,
(amp, p) => createAmp(amp.out, p),
new Computer() // A default computer has output of 0 which initialises the first of the amplifiers
);

while (amps[amps.length - 1].status.label === "AwaitingInput") {
for (let i = 0; i < amps.length; i++) {
amps[i].addInput(amps[(i - 1 + amps.length) % amps.length].out).run();
}
}
return amps[amps.length - 1].out;
}
Insert cell
Insert cell
function part2(input) {
return Math.max(...AOC.permutations([5, 6, 7, 8, 9]).map(generateSignal));
}
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