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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more