|
@@ -0,0 +1,58 @@
|
|
|
+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 sum of the powers of all games
|
|
|
+ *
|
|
|
+ * The power of a game is the smallest amount of each color of cube
|
|
|
+ * that could be in bag to allow a game to be valid multiplied by each
|
|
|
+ * other.
|
|
|
+ */
|
|
|
+let powerSum = 0;
|
|
|
+
|
|
|
+for(const game of parsedGames) {
|
|
|
+ const smallestSet = FindFewestValidCubes(game);
|
|
|
+
|
|
|
+ const powerOfSet = (smallestSet.red || 1) * (smallestSet.green || 1) * (smallestSet.blue || 1)
|
|
|
+
|
|
|
+ powerSum += powerOfSet;
|
|
|
+}
|
|
|
+
|
|
|
+console.log(`The sum power of the games was: ${powerSum}`);
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Find the fewest possible cubes in the bag
|
|
|
+ *
|
|
|
+ * Goes through all sets in a game to find the fewest possible amount
|
|
|
+ * of each colored cube for that game to be valid.
|
|
|
+ *
|
|
|
+ * @param {Game} parsedGame A game played
|
|
|
+ */
|
|
|
+function FindFewestValidCubes (parsedGame: Game): Set {
|
|
|
+ const smallestSet: Set = {
|
|
|
+ red: 0,
|
|
|
+ green: 0,
|
|
|
+ blue: 0
|
|
|
+ };
|
|
|
+
|
|
|
+ for (const set of parsedGame.Sets) {
|
|
|
+ if (set.red > smallestSet.red) { smallestSet.red = set.red; }
|
|
|
+ if (set.green > smallestSet.green) { smallestSet.green = set.green; }
|
|
|
+ if (set.blue > smallestSet.blue) { smallestSet.blue = set.blue; }
|
|
|
+ }
|
|
|
+
|
|
|
+ return smallestSet;
|
|
|
+}
|