Public
Edited
Dec 2, 2023
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input
.split("\n")
.map((row) =>
Array.from(row)
.filter((c) => parseInt(c))
.map(Number)
)
.map((row) => row[0] * 10 + row[row.length - 1]);
}
Insert cell
Insert cell
function part1(input) {
return _.sum(parse(input))
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function wordToDigits(input) {
let result = "";
while (input.length) {
const match = input.match(
/^(one|two|three|four|five|six|seven|eight|nine|\d)/
);

if (match) {
const i = parseInt(match[0]);
if (isNaN(i)) {
// If the input starts with a word, find its corresponding value
result += digits[match[0]];
} else {
// If the input starts with a digit, add it to the result
result += i;
}
} else {
result += input[0];
}
input = input.slice(1);
}
return result;
}
Insert cell
function part2(input) {
return _.sum(parse(wordToDigits(input)));
}
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