Public
Edited
Dec 25
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
const edges = input.split("\n").map((d) => d.split("-"));

const g = new Graph();
const nodes = [...new Set(edges.flat())];
edges.forEach((edge) => g.addUndirectedEdge(...edge));
return { nodes, g };
}
Insert cell
Insert cell
Insert cell
Insert cell
function triangles(n, g) {
return AOC.pairwiseCombinations(g.adjacent(n))
.filter((edge) => g.hasEdge(...edge))
.map((d) => [n, ...d].sort());
}
Insert cell
Insert cell
function part1(input) {
const { nodes, g } = parse(input);
const tris = nodes.map((n) => triangles(n, g)).flat();
return tris.filter((tri) => tri.some((d) => d.startsWith("t"))).length / 3;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const { nodes, g } = parse(input);

const tris = nodes.map((n) => triangles(n, g));
const maxSize = d3.max(tris, (d) => d.length);
const notMaxClique = new Set(tris.filter((d) => d.length < maxSize).flat(2));

return [...AOC.difference(new Set(nodes), notMaxClique)].sort().join(",");
}
Insert cell
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