Published
Edited
Dec 10, 2021
1 star
Insert cell
Insert cell
part2 = d3.median(
incomplete.map((d) =>
complete(d)
.map((d) => score2.get(d))
.reduce((score, n) => score * 5 + n, 0)
)
)
Insert cell
score2 = new Map([[")", 1], ["]", 2], ["}", 3], [">", 4]])
Insert cell
complete = (line) => {
const stack = [];
for (const ch of line) {
if (pairs.has(ch))
stack.unshift(ch);
else if (pairs.get(stack[0]) === ch)
stack.shift();
}
return stack.map((d) => pairs.get(d));
}
Insert cell
incomplete = lines.filter((d) => !corrupt(d))
Insert cell
part1 = d3.sum(lines.map(corrupt).map((d) => score.get(d)))
Insert cell
corrupt = (line) => {
const stack = [];
for (const ch of line) {
if (pairs.has(ch))
stack.unshift(ch);
else if (pairs.get(stack[0]) === ch)
stack.shift();
else
return ch;
}
return false;
}
Insert cell
score = new Map([[")", 3], ["]", 57], ["}", 1197], [">", 25137]])
Insert cell
pairs = new Map([["[", "]"], ["(", ")"], ["{", "}"], ["<", ">"]])
Insert cell
lines = (which === "test" ? test : input).trim().split("\n")
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