Public
Edited
Dec 1, 2023
Insert cell
Insert cell
sample = `
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
`
Insert cell
part1 = d3.sum(
input
.trim()
.split("\n")
.map((l) => {
const numbers = [...l].filter(Number);
return Number(numbers.at(0) + numbers.at(-1));
})
)
Insert cell
sample2 = `
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
`
Insert cell
part2 = d3.sum(
input
.trim()
.split("\n")
.map((l) => {
const numbers = [];
let s = "";
for (const c of l) {
if (!isNaN(+c)) numbers.push(c);
s += c;
for (const [i, name] of names.entries())
if (s.endsWith(name)) numbers.push(String(i + 1));
}
return Number(numbers.at(0) + numbers.at(-1));
})
)
Insert cell
names = "one two three four five six seven eight nine".trim().split(" ")
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