Public
Edited
Dec 1, 2023
Insert cell
Insert cell
Insert cell
states = ({
inGroup: "inGroup",
inGarbage: "inGarbage",
ignoreNext: "ignoreNext"
})
Insert cell
Insert cell
function processStream(stream) {
let state = states.inGroup;
let [depth, score, gCount] = [0, 0, 0];

for (const char of stream) {
switch (state) {
case states.inGroup:
if (char === "{") {
depth++;
} else if (char === "}") {
score += depth--;
} else if (char === "<") {
state = states.inGarbage;
}
break;

case states.inGarbage:
if (char === ">") {
state = states.inGroup;
} else if (char === "!") {
state = states.ignoreNext;
} else {
gCount++;
}
break;

case states.ignoreNext:
state = states.inGarbage;
break;
}
}
return { score, gCount };
}
Insert cell
function part1(input) {
return processStream(input).score;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
return processStream(input).gCount;
}
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