this repo has no description
0
fork

Configure Feed

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

part 1 done!

+52 -4
+44 -2
2023/day02/part1.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 + const bag = { r: 12, g: 13, b: 14 }; 14 + return games 15 + .filter((g) => isGamePossible(g, bag)) 16 + .reduce((sum, g) => sum + g.id, 0); 17 + } 18 + 19 + function isGamePossible(g: Game, bag: Round): boolean { 20 + const colors: ColorShort[] = ["r", "g", "b"]; 21 + return colors.every((c) => g.rounds.every((r: Round) => r[c] <= bag[c])); 22 + } 23 + 24 + type Game = { id: number; rounds: Round[] }; 25 + type Round = Record<ColorShort, number>; 26 + type ColorShort = "r" | "g" | "b"; 27 + type Color = "red" | "green" | "blue"; 28 + 29 + function parseGame(l: string): Game { 30 + const id = parseInt(l.substring(4, l.indexOf(":"))); 31 + return { id, rounds: parseRounds(l) }; 32 + } 33 + 34 + function parseRounds(l: string): Round[] { 35 + return l 36 + .substring(l.indexOf(": ") + 1) 37 + .split("; ") 38 + .map(parseRound); 39 + } 40 + 41 + function parseRound(rStr: string): Round { 42 + const countForColor = (c: Color) => 43 + parseInt( 44 + rStr 45 + .trim() 46 + .split(", ") 47 + .find((draw) => draw.endsWith(c)) 48 + ?.replace(` ${c}`, "") ?? "0", 49 + ); 50 + return { 51 + r: countForColor("red"), 52 + b: countForColor("blue"), 53 + g: countForColor("green"), 54 + }; 13 55 }
+8 -2
2023/day02/test.ts
··· 3 3 import * as p2 from "./part2.ts"; 4 4 5 5 Deno.test("part1", () => { 6 - const examples = ["abc", "def"].join("\n"); 7 - assertEquals(p1.answer(examples), 42); 6 + const examples = [ 7 + "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", 8 + "Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue", 9 + "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red", 10 + "Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red", 11 + "Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", 12 + ].join("\n"); 13 + assertEquals(p1.answer(examples), 8); 8 14 }); 9 15 10 16 Deno.test("part2", () => {