Public
Edited
Nov 14, 2022
1 star
Insert cell
Insert cell
Insert cell
pp = import("https://cdn.skypack.dev/pratt-parser@8.1.0?min")
Insert cell
Insert cell
function value(v) {
return Object.create(null, { value: { value: v } });
}
Insert cell
Insert cell
function createGrammar(addPrecedence, multPrecedence) {
return new pp.Parser({
tokens: [pp.WhiteSpaceToken, pp.NumberToken],
prefix: {
"(": {
nud(grammar) {
const e = grammar.expression(0);
grammar.advance(")");
return e;
}
}
},
infix: {
")": {},
"+": {
precedence: addPrecedence,
combine: (left, right) => value(left.value + right.value)
},
"*": {
precedence: multPrecedence,
combine: (left, right) => value(left.value * right.value)
}
}
});
}
Insert cell
Insert cell
function part1(input) {
const grammar = createGrammar(1, 1);
return AOC.sum(puzzleInput.map((expr) => grammar.parse(expr).value));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const grammar = createGrammar(2, 1);
return AOC.sum(puzzleInput.map((expr) => grammar.parse(expr).value));
}
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