Published
Edited
Dec 6, 2021
7 stars
Insert cell
Insert cell
Insert cell
Insert cell
extent = d3.extent(state, (d) => d.t)
Insert cell
state = {
const next = boards.slice();
for (let t = 0; t <= until; t++) {
for (let i = 0; i < next.length; i++)
next[i] = complete(next[i]) ? next[i] : mark(numbers[t], next[i]);
for (const board of next.filter(complete)) board.t ||= t;
}
return next;
}
Insert cell
part2 = {
let next = boards.slice();
for (const n of numbers) {
for (let i = 0; i < next.length; i++) next[i] = mark(n, next[i]);
const remaining = next.filter((d) => !complete(d));
if (remaining.length == 0) return score(n, next[0]);
next = remaining;
}
}
Insert cell
part1 = {
const next = boards.slice();
for (const n of numbers) {
for (let i = 0; i < next.length; i++) {
next[i] = mark(n, next[i]);
if (complete(next[i])) return score(n, next[i]);
}
}
}
Insert cell
score = (n, board) => {
const sum = board
.flatMap((row) => row.filter((d) => d))
.reduce((sum, d) => sum + d);
return n * sum;
}
Insert cell
mark = (n, board) => board.map((row) => row.map((d) => (d === n ? null : d)))
Insert cell
complete = (board) => {
for (const r of board) if (r.every((d) => d === null)) return true;
for (const c of d3.transpose(board)) if (c.every((d) => d === null)) return true;
return false;
}
Insert cell
Insert cell
numbers = data.split("\n")[0].split(",").map(Number)
Insert cell
boards = data
.split("\n\n")
.slice(1)
.map((b) => b.split("\n").map((l) => l.trim().split(/\s+/).map(Number)))
Insert cell
data = (which === "test" ? test : input).trim()
Insert cell
Insert cell
input = FileAttachment("input.txt").text()
Insert cell
import { Scrubber } from "@mbostock/scrubber"
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