Public
Edited
Dec 6, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
games = {
// Use a label so we can stop iterating the game runs if its invalid
const validGames = [];
skipGame: for (let i of inputArr) {
const [gameName, runs] = i.split(":");
let run = runs.split(";");

for (let line of run) {
const hand = line.split(",");

for (let g of hand) {
let [num, colour] = g.trim().split(" ");
num = parseInt(num, 10);
//console.log(num, colour);
if (!isValidGame(colour, num)) {
continue skipGame; // This will stop looping and continue parent loop
}
}
}
const [gameNumOnly] = gameName.match(/\d+$/);

validGames.push(parseInt(gameNumOnly, 10));
}

return validGames;
}
Insert cell
games.reduce((acc, g) => {
acc = acc + g;

return acc;
}, 0)
Insert cell
isValidGame = (colour, num) => {
const lookyloo = { red: 12, green: 13, blue: 14 };

return num <= lookyloo[colour];
}
Insert cell
Insert cell
inputArr.map((i) => {
const trackVals = { red: 0, green: 0, blue: 0 };
const [, game] = i.split(":");
const runs = game.split(";");
debugger;
return runs.reduce(
(acc, run) => {
const vals = run.match(/\d+ \w+/g);
vals.forEach((val) => {
let [v, k] = val.split(" ");
v = parseInt(v, 10);

if (v > trackVals[k]) {
acc[k] = v;
trackVals[k] = v;
}
});

if

return acc;
},
{ red: 0, green: 0, blue: 0 }
);
})
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