Public
Edited
Sep 26, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
function reallocate(memText) {
const memory = memText.split("\t").map(Number);
const i = d3.maxIndex(memory);
const distrib = Math.floor(memory[i] / 16); // Blocks each memory bank will receive
const remainder = memory[i] % 16; // Remaining blocks to distribute
memory[i] = 0; // Reset the value of the max bank

// Distribute the bulk of the blocks directly
for (let j = 0; j < 16; j++) {
memory[j] += distrib;
}
// Distribute the remaining blocks
for (let j = 1; j <= remainder; j++) {
memory[(i + j) % 16]++;
}
return memory.join("\t"); // Store result back as a string.
}
Insert cell
Insert cell
function part1(input) {
const [s0, s1] = AOC.sequenceCycle(10000, input, reallocate);
return s0 + s1;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
return AOC.sequenceCycle(10000, input, reallocate)[0];
}
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