
Day 7, part 2
Day 7 challenge of the mighty Advent of Code 2024
Introduction
Welcome to the par two of day 7 puzzle. First part is described in Day 7, part 1. Please get familiar with the part 1 before jumping into this article. This part of the challenge is available only when you successfully finished the part 1. The second part of the challenge is described under -- Part Two -- section here.
What is the new problem
Now some rules have changed. The J card is now treated as a wildcard and it pretends to be the card that gives the best type so QJJQ2 is treated as QQQQ2. The second rule for J is that even tho it is pretending to be some other card, when it comes to comparing individual card strengths, it is now the weakest card, weaker even than 2. Our goal is again to find the sum of bids, but now we have to incorporate those new rules.
Solution
The first thing we can easily do is to change strength of J card in cardStrengths object so in the card comparison step when we have two hands with the same type and we are comparing the cards themselves one by one, the J card will have the lowest strength. This will fulfill one of the new rules. The second thing is to somehow find the best card in the hand to replace the J with and use this to properly sort and rank each hand.
Find and replace J
This is a pretty complex task. It has to be broken into steps:
- We need to get the card counts using the
countCardsInHandfunction. - We replace the number of
Js in the count with0so it's not taken into consideration in the next step. - Based on the card counts we find the best card to replace the
Jwith by finding the keys with the biggest number of cards. - From those keys, we pick one by checking card strengths.
- Finally we replace all the cards
Js with our best key we found. Looking at our test data we can expect the following results:
132T3K -> stays the same cause there is no J
2T55J5 -> T5555
3KK677 -> stays the same cause there is no J
4KTJJT -> KTTTT
5QQQJA -> QQQQTIn code this can be done like so:
1function updateJInCardHand(cardHand) {
2 const cardCounts = countCardsInHand(cardHand);
3 const newCardCounts = { ...cardCounts, J: 0 };
4 const highestKeys = findHighestCountKeys(newCardCounts);
5 const bestFitForJ = getBestKeyForJ(highestKeys);
6 const newCards = replaceJCards(cardHand.cards, bestFitForJ);
7 return { ...cardHand, cards: newCards };
8}
9function findHighestCountKeys(cardCounts) {
10 let highestCount = 0;
11 for (const cardKey in cardCounts) {
12 if (cardCounts[cardKey] > highestCount) {
13 highestCount = cardCounts[cardKey];
14 }
15 }
16 const result = [];
17 Object.entries(cardCounts).forEach(([key, value]) => {
18 if (value === highestCount) {
19 result.push(key);
20 }
21 });
22 return result;
23}
24function getBestKeyForJ(highestKeys) {
25 let bestKey = `J`;
26 highestKeys.forEach((key) => {
27 if (cardStrengths[bestKey] < cardStrengths[key]) {
28 bestKey = key;
29 }
30 });
31 return bestKey;
32}
33function replaceJCards(cards, bestFitKey) {
34 return cards.map((card) => {
35 if (card === 'J') {
36 return bestFitKey;
37 }
38 return card;
39 });
40}
41const jUpdatedCardHands = inputData.map(updateJInCardHand);
42console.log('Hands with updated J: ', jUpdatedCardHands);And if we run the code this is what will be printed:
1Hands with updated J: [
2 { cards: [ '3', '2', 'T', '3', 'K' ], bid: 765 },
3 { cards: [ 'T', '5', '5', '5', '5' ], bid: 684 },
4 { cards: [ 'K', 'K', '6', '7', '7' ], bid: 28 },
5 { cards: [ 'K', 'T', 'T', 'T', 'T' ], bid: 220 },
6 { cards: [ 'Q', 'Q', 'Q', 'Q', 'A' ], bid: 483 }
7]Look closely, what we get here is what we were looking for! Lets save it.
1git add .
2git commit -m "replace j card with other best card"Updating the type getting
Now we have the new hands with all the Js replaced by the card that makes the hand the strongest possible. Thanks to that we can find a type for every hand. If we analyse the data from the previous step, this are the new types (I assign them to the initial hands):
132T3K -> one-pair
2T55J5 -> four-of-kind
3KK677 -> two-pairs
4KTJJT -> four-of-kind
5QQQJA -> four-of-kindFor updating the hand types we will again use the getCardHandTypes function which I decided to rename to updateHandTypes cause it was more clear to me.
1function updateHandTypes(cardHands, jUpdatedCardHands, cardTypes) {
2 return cardHands.map((cardHand, idx) => ({
3 ...cardHand,
4 type: getCardHandType(jUpdatedCardHands[idx], cardTypes),
5 }));
6}As you can see now it takes as a parameter the hands with replaced J. These are used to determine the hand type now. We can run our program to now see in the terminal all our hands but with the new types.
1Card hands with types: [
2 {
3 cards: [ '3', '2', 'T', '3', 'K' ],
4 bid: 765,
5 type: 'one-pair'
6 },
7 {
8 cards: [ 'T', '5', '5', 'J', '5' ],
9 bid: 684,
10 type: 'four-of-kind'
11 },
12 {
13 cards: [ 'K', 'K', '6', '7', '7' ],
14 bid: 28,
15 type: 'two-pairs'
16 },
17 {
18 cards: [ 'K', 'T', 'J', 'J', 'T' ],
19 bid: 220,
20 type: 'four-of-kind'
21 },
22 {
23 cards: [ 'Q', 'Q', 'Q', 'J', 'A' ],
24 bid: 483,
25 type: 'four-of-kind'
26 }
27]This is the expected result. We can commit the changes.
1git add .
2git commit -m "get hand type based on best cards"The new final result
Actually running the whole program as we did a few seconds before, we got also the new final answer printed. If we check the expected result on the puzzles website we can find that it is 5905 now. And if we take a look at the terminal we see:
1Sum of bids: 5905so this is again what we were looking for. Hooray!
Final result
Now we can switch back to the input being read from the file and we can make the final run of our code.
1Sum of bids: 248781813We now submit this number on the page and we can see our favorite message.
Lets commit all the changes.
1git add .
2git commit -m "day 7 part 2 final result"And we nailed it once again! Hope you enjoyed the puzzle and see you in Day 8, part 1