Public
Edited
Dec 2, 2024
Paused
2 forks
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function regHistory(input) {
const execCmd = (cmd, [reg, i]) => {
const [op, val] = cmd.split(" ");
if (op === "noop") {
reg[i + 1] = reg[i];
return [reg, i + 1];
}
reg[i + 1] = reg[i];
reg[i + 2] = reg[i] + Number(val);
return [reg, i + 2];
};

const [history, i] = input
.split("\n")
.reduce(([reg, i], cmd) => execCmd(cmd, [reg, i]), [[1, 1], 1]);
return history;
}
Insert cell
Insert cell
function part1(input) {
const reg = regHistory(input);
return AOC.sum([20, 60, 100, 140, 180, 220].map((t) => reg[t] * t));
}
Insert cell
Insert cell
Insert cell
Insert cell
function draw(reg) {
const crt = AOC.gInit(6, 40, " ");
for (let t = 1; t <= 240; t++) {
const [row, col] = [Math.floor((t - 1) / 40), (t - 1) % 40];
if (Math.abs(col - reg[t]) <= 1) {
crt[row][col] = "█";
}
}
return crt.map((row) => row.join("")).join("\n");
}
Insert cell
```
${draw(regHistory(puzzleInput))}
```
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

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