Public
Edited
Dec 10, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cycles = {
var X = 1;
return input.flatMap(({ instr, val }) => {
if (instr == "addx") {
const currX = X;
X += val;
return [
{ instr: instr, opCycle: 1, opVal: val, duringX: currX, afterX: currX },
{ instr: instr, opCycle: 2, opVal: val, duringX: currX, afterX: X }
];
} else {
return [{ instr: instr, opCycle: 1, opVal: 0, duringX: X, afterX: X }];
}
});
}
Insert cell
message = {
return [0, 40, 80, 120, 160, 200] // cheat our way making rows since we know max row/col ahead of time
.map((cycle) => {
const row = " ".repeat(40).split(""); // row initially blank
_.range(0 + cycle, 40 + cycle).forEach((v, i) => { // as each pixel is visited
// again, kind of wasteful, but makes the solution easier to read
const spritePos = [
cycles[v].duringX - 1,
cycles[v].duringX,
cycles[v].duringX + 1
];
if (spritePos.includes(i)) row[i] = "░";
});
return row.join("");
})
.join("\n");
}
Insert cell
Insert cell
pixels = {
return [0, 40, 80, 120, 160, 200]
.flatMap((cycle, r) => {
const row = " ".repeat(40).split("");
return _.range(0 + cycle, 40 + cycle).flatMap((v, c) => {
const spritePos = [
cycles[v].duringX - 1,
cycles[v].duringX,
cycles[v].duringX + 1
];
if (spritePos.includes(c)) return { x: c, y: r };
});
})
.filter((d) => d);
}
Insert cell
Insert cell
cycles
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
input00 = (await FileAttachment("input00.txt").text()).trim()
Insert cell
input01 = (await FileAttachment("input01.txt").text()).trim()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
img = {
elem; // this gets us reactivity
var node = document.getElementById("test");
const image = new Image();
image.height = 60;
image.src = await domtoimage.toPng(node, { width: 400, height: 80 });
return image;
}
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