Public
Edited
Dec 5, 2023
Importers
Insert cell
AOC.title(meta.year, meta.day, meta.title)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
AOC.resultTable(meta)
Insert cell
part1 = (s) => AOC.sum(s.map(integers).map(firstAndLast).map(Number))
Insert cell
Insert cell
part2 = (s) =>
AOC.sum(s.map(lettersToDigits).map(integers).map(firstAndLast).map(Number))
Insert cell
integers = (sentence) => {
const words = sentence.split("");
return words
.filter(function (w) {
return w.match(/\d+/);
})
.map(Number);
}
Insert cell
lettersToDigits = (s) =>
s
.replace(/^\D*twone/, "2ne")
.replace(/^\D*eightwo/, "8wo")
.replace(/^\D*eighthree/, "8hree")
.replace(/^\D*nineight/, "9ight")
.replace(/oneight\D*$/, "on8")
.replace(/threeight\D*$/, "thre8")
.replace(/fiveight\D*$/, "fiv8")
.replace(/one/g, "1")
.replace(/two/g, "2")
.replace(/three/g, "3")
.replace(/four/g, "4")
.replace(/five/g, "5")
.replace(/six/g, "6")
.replace(/seven/g, "7")
.replace(/eight/g, "8")
.replace(/nine/g, "9")
Insert cell
firstAndLast = (digits) => "" + digits[0] + digits[digits.length - 1]
Insert cell
data.map(lettersToDigits)
Insert cell
data = parse(which === "test1" ? test1 : input)
Insert cell
parse = (s) => s.trim().split("\n")
Insert cell
Insert cell
Insert cell
inputs = ({
test: test1,
real: input
})
Insert cell
meta = AOC.meta({
day: 4,
year: 23,
title: "Scratchcards",
parse,
inputs,
parts: [part1, part2],
expected: { test: [13, 30], real: [22897, 5095824] },
href: "https://observablehq.com/@a1ip/aoc-2023-day-4"
})
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