Public
Edited
Dec 2, 2023
Insert cell
Insert cell
Insert cell
getPossibleGames(test);
Insert cell
getPossibleGames(test).reduce((sum, gameId) => sum + gameId);
Insert cell
Insert cell
getMinPossibleGames(test)
Insert cell
getMinPossibleGames(test).map((row) => {
const rowValues = Object.values(row);
return rowValues.reduce((acc, val) => acc * val, 1);
}).reduce((acc, product) => acc + product, 0);
Insert cell
Insert cell
getPossibleGames(data);
Insert cell
getPossibleGames(data).reduce((sum, gameId) => sum + gameId);
Insert cell
getMinPossibleGames(data)
Insert cell
getMinPossibleGames(data).map((row) => {
const rowValues = Object.values(row);
return rowValues.reduce((acc, val) => acc * val, 1);
}).reduce((acc, product) => acc + product, 0);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function getMin(game) {
let id = game.split(":")[0];
let data = game.split(":")[1].split(";");

const mapped = data.map((d) => {
return d
.trim()
.split(", ")
.map((cube) => {
const [count, color] = cube.split(" ");
return { color: color, value: +count };
});
});

const minValues = {};

mapped.flat().forEach((item) => {
const { color, value } = item;

if (!minValues[color] || value >= minValues[color]) {
minValues[color] = value;
}
});

return minValues
}
Insert cell
function getMinPossibleGames(input) {
return input
.map((game) => {
const gameId = parseInt(game.match(/\d+/)[0], 10);
return getMin(game);
return getMin(game) ? gameId : null;
})
.filter((gameId) => gameId !== null);
}
Insert cell
function calculatePower(cubeCounts) {
return cubeCounts.red * cubeCounts.green * cubeCounts.blue;
}
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