this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

part 2 done!

+93 -6
+50 -2
2023/day02/part2.ts
··· 8 8 } 9 9 10 10 export function answer(input: string): number { 11 - console.log(input); 12 - return 42; 11 + const games = input.split("\n").map(parseGame); 12 + 13 + return games 14 + .map((g) => minCubes(g.rounds)) 15 + .reduce((sum, r) => sum + power(r), 0); 16 + } 17 + 18 + function minCubes(rounds: Round[]): Round { 19 + return { 20 + r: Math.max(...rounds.map(({ r }) => r)), 21 + g: Math.max(...rounds.map(({ g }) => g)), 22 + b: Math.max(...rounds.map(({ b }) => b)), 23 + }; 24 + } 25 + 26 + function power({ r, g, b }: Round): number { 27 + return r * g * b; 28 + } 29 + 30 + type Game = { id: number; rounds: Round[] }; 31 + type Round = Record<ColorShort, number>; 32 + type ColorShort = "r" | "g" | "b"; 33 + type Color = "red" | "green" | "blue"; 34 + 35 + function parseGame(l: string): Game { 36 + const id = parseInt(l.substring(4, l.indexOf(":"))); 37 + return { id, rounds: parseRounds(l) }; 38 + } 39 + 40 + function parseRounds(l: string): Round[] { 41 + return l 42 + .substring(l.indexOf(": ") + 1) 43 + .split("; ") 44 + .map(parseRound); 45 + } 46 + 47 + function parseRound(rStr: string): Round { 48 + const countForColor = (c: Color) => 49 + parseInt( 50 + rStr 51 + .trim() 52 + .split(", ") 53 + .find((draw) => draw.endsWith(c)) 54 + ?.replace(` ${c}`, "") ?? "0", 55 + ); 56 + return { 57 + r: countForColor("red"), 58 + b: countForColor("blue"), 59 + g: countForColor("green"), 60 + }; 13 61 }
+35 -2
2023/day02/puzzle.md
··· 30 30 31 31 Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. *What is the sum of the IDs of those games?* 32 32 33 - To begin, [get your puzzle input](2/input). 33 + Your puzzle answer was `2795`. 34 + 35 + The first half of this puzzle is complete! It provides one gold star: \* 36 + 37 + \--- Part Two --- 38 + ---------- 39 + 40 + The Elf says they've stopped producing snow because they aren't getting any *water*! He isn't sure why the water stopped; however, he can show you how to get to the water source to check it out for yourself. It's just up ahead! 41 + 42 + As you continue your walk, the Elf poses a second question: in each game you played, what is the *fewest number of cubes of each color* that could have been in the bag to make the game possible? 43 + 44 + Again consider the example games from earlier: 45 + 46 + ``` 47 + Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green 48 + Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue 49 + Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red 50 + Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red 51 + Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green 52 + 53 + ``` 54 + 55 + * In game 1, the game could have been played with as few as 4 red, 2 green, and 6 blue cubes. If any color had even one fewer cube, the game would have been impossible. 56 + * Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes. 57 + * 58 + * Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes. 59 + * Game 4 required at least 14 red, 3 green, and 15 blue cubes. 60 + * Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag. 61 + 62 + The *power* of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. The power of the minimum set of cubes in game 1 is `48`. In games 2-5 it was `12`, `1560`, `630`, and `36`, respectively. Adding up these five powers produces the sum `*2286*`. 63 + 64 + For each game, find the minimum set of cubes that must have been present. *What is the sum of the power of these sets?* 34 65 35 66 Answer: 36 67 37 - You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Cube+Conundrum%22+%2D+Day+2+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F2&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. 68 + Although it hasn't changed, you can still [get your puzzle input](2/input). 69 + 70 + You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Cube+Conundrum%22+%2D+Day+2+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F2&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle.
+8 -2
2023/day02/test.ts
··· 14 14 }); 15 15 16 16 Deno.test("part2", () => { 17 - const examples = ["abc", "def"].join("\n"); 18 - assertEquals(p2.answer(examples), 42); 17 + const examples = [ 18 + "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", 19 + "Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue", 20 + "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red", 21 + "Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red", 22 + "Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", 23 + ].join("\n"); 24 + assertEquals(p2.answer(examples), 2286); 19 25 });