Public
Edited
Dec 13, 2024
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input.split("\n\n").map((d) => {
const [xa, ya, xb, yb, x, y] = d.match(/\d+/g).map(Number);
return { xa, ya, xb, yb, x, y };
});
}
Insert cell
Insert cell
function solve(claws, offset = 0) {
const input = testData;
const presses = [];
claws.forEach((d) => {
const det = d.xa * d.yb - d.ya * d.xb;
const [x, y] = [d.x + offset, d.y + offset];
const a = (x * d.yb - y * d.xb) / det;
const b = (d.xa * y - d.ya * x) / det;
if (a > 0 && Number.isInteger(a) && b > 0 && Number.isInteger(b)) {
presses.push({ a, b });
}
});
return presses;
}
Insert cell
Insert cell
function part1(input) {
return d3.sum(solve(parse(input)), (d) => 3 * d.a + d.b);
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
return d3.sum(solve(parse(input), 1e13), (d) => d.a * 3 + d.b);
}
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