123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import { LoadInput } from "../common.ts";
- import { ParseGameStrings, type Game, type Set } from "./2_common.ts";
- const test = [
- "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green",
- "Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue",
- "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red",
- "Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red",
- "Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green",
- ];
- const input = await LoadInput(2);
- const parsedGames = ParseGameStrings(input);
- /**
- * The target amount of each colored cube in a games bag
- *
- * As reported by the elf, only games with the maximum amount of each
- * color of cube specified here are valid
- */
- const maxCubes: Set = {
- red: 12,
- green: 13,
- blue: 14,
- };
- let sumOfValidIDs = 0;
- for (const game of parsedGames) {
- if (ValidateGames(game, maxCubes)) {
- sumOfValidIDs += game.ID;
- }
- }
- console.log(`The sum of valid game ID's is: ${sumOfValidIDs}`);
- /**
- * Validate whether a game was possible
- *
- * A game is only valid if none of the sets in the game played
- * contained more of than the target maximum for that color.
- *
- * @param {Game} gameToValidate A game to validate
- * @param {Set} targetMaximum The maximum of each color in a set required for a game to be valid
- */
- function ValidateGames(gameToValidate: Game, targetMaximum: Set): boolean {
- for(const set of gameToValidate.Sets) {
- if (set.red > targetMaximum.red
- || set.green > targetMaximum.green
- || set.blue > targetMaximum.blue) { return false; }
- }
- return true;
- }
|