this repo has no description
0
fork

Configure Feed

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

part 2 done!

+87 -7
+34 -2
2023/day04/part2.ts
··· 7 7 console.log(answer(input)); 8 8 } 9 9 10 + type card = { wNums: number[]; nums: number[]; copies: number }; 11 + 10 12 export function answer(input: string): number { 11 - console.log(input); 12 - return 42; 13 + const cards = input.split("\n").map(parseCard); 14 + 15 + for (let c = 0; c < cards.length; c++) { 16 + const matches = numMatches(cards[c]); 17 + for (let i = 0; i < cards[c].copies; i++) { 18 + for (let cn = 1; cn <= matches; cn++) { 19 + cards[c + cn].copies++; 20 + } 21 + } 22 + } 23 + 24 + return cards.reduce((sum, c) => sum + c.copies, 0); 25 + } 26 + 27 + function numMatches({ wNums, nums }: card): number { 28 + return nums.filter((n) => wNums.includes(n)).length; 29 + } 30 + 31 + function parseCard(line: string): card { 32 + const [wNums, nums] = line 33 + .substring(line.indexOf(":") + 1) 34 + .split(" | ") 35 + .map((part) => 36 + part 37 + .trim() 38 + .split(" ") 39 + .filter((s) => s.trim().match(/\d+/)) 40 + .map((n) => { 41 + return parseInt(n.trim()); 42 + }), 43 + ); 44 + return { wNums, nums, copies: 1 }; 13 45 }
+44 -3
2023/day04/puzzle.md
··· 39 39 40 40 Take a seat in the large pile of colorful cards. *How many points are they worth in total?* 41 41 42 - To begin, [get your puzzle input](4/input). 42 + Your puzzle answer was `26218`. 43 + 44 + \--- Part Two --- 45 + ---------- 46 + 47 + Just as you're about to report your findings to the Elf, one of you realizes that the rules have actually been printed on the back of every card this whole time. 48 + 49 + There's no such thing as "points". Instead, scratchcards only cause you to *win more scratchcards* equal to the number of winning numbers you have. 50 + 51 + Specifically, you win *copies* of the scratchcards below the winning card equal to the number of matches. So, if card 10 were to have 5 matching numbers, you would win one copy each of cards 11, 12, 13, 14, and 15. 52 + 53 + Copies of scratchcards are scored like normal scratchcards and have the *same card number* as the card they copied. So, if you win a copy of card 10 and it has 5 matching numbers, it would then win a copy of the same cards that the original card 10 won: cards 11, 12, 13, 14, and 15. This process repeats until none of the copies cause you to win any more cards. (Cards will never make you copy a card past the end of the table.) 54 + 55 + This time, the above example goes differently: 56 + 57 + ``` 58 + Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 59 + Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 60 + Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 61 + Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 62 + Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 63 + Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 64 + 65 + ``` 66 + 67 + * Card 1 has four matching numbers, so you win one copy each of the next four cards: cards 2, 3, 4, and 5. 68 + * Your original card 2 has two matching numbers, so you win one copy each of cards 3 and 4. 69 + * Your copy of card 2 also wins one copy each of cards 3 and 4. 70 + * Your four instances of card 3 (one original and three copies) have two matching numbers, so you win *four* copies each of cards 4 and 5. 71 + * Your eight instances of card 4 (one original and seven copies) have one matching number, so you win *eight* copies of card 5. 72 + * Your fourteen instances of card 5 (one original and thirteen copies) have no matching numbers and win no more cards. 73 + * Your one instance of card 6 (one original) has no matching numbers and wins no more cards. 74 + 75 + Once all of the originals and copies have been processed, you end up with `*1*` instance of card 1, `*2*` instances of card 2, `*4*` instances of card 3, `*8*` instances of card 4, `*14*` instances of card 5, and `*1*` instance of card 6. In total, this example pile of scratchcards causes you to ultimately have `*30*` scratchcards! 76 + 77 + Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, *how many total scratchcards do you end up with?* 78 + 79 + Your puzzle answer was `9997537`. 80 + 81 + Both parts of this puzzle are complete! They provide two gold stars: \*\* 82 + 83 + At this point, you should [return to your Advent calendar](/2023) and try another puzzle. 43 84 44 - Answer: 85 + If you still want to see it, you can [get your puzzle input](4/input). 45 86 46 - You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Scratchcards%22+%2D+Day+4+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F4&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. 87 + You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+%22Scratchcards%22+%2D+Day+4+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F4&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle.
+9 -2
2023/day04/test.ts
··· 15 15 }); 16 16 17 17 Deno.test("part2", () => { 18 - const examples = ["abc", "def"].join("\n"); 19 - assertEquals(p2.answer(examples), 42); 18 + const examples = [ 19 + "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53", 20 + "Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19", 21 + "Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1", 22 + "Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83", 23 + "Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36", 24 + "Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", 25 + ].join("\n"); 26 + assertEquals(p2.answer(examples), 30); 20 27 });