Public
Edited
Dec 2, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input.split("\n").map((game) =>
game
.split(":")[1]
.split(";")
.flatMap((bag) =>
bag
.split(",")
.map((entry) => entry.trim().split(" "))
.map(([count, name]) => [parseInt(count), name])
)
);
}
Insert cell
Insert cell
function isPossible(game) {
return !game.some(
([count, cube]) =>
(cube === "red" && count > 12) ||
(cube === "green" && count > 13) ||
(cube === "blue" && count > 14)
);
}
Insert cell
Insert cell
function part1(input) {
const games = parse(input);
return _.sum(games.map((game, i) => (isPossible(game) ? i + 1 : 0)));
}
Insert cell
Insert cell
Insert cell
Insert cell
function powerSet(game) {
const groupedCubes = _.groupBy(game, (d) => d[1]);
const maxCubes = _.map(groupedCubes, (cubes) => _.maxBy(cubes, 0)[0]);
return maxCubes.reduce((a, b) => a * b, 1);
}
Insert cell
Insert cell
function part2(input) {
const games = parse(input);
return _.sum(games.map(powerSet));
}
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