Skip to content

Commit 8a41c5a

Browse files
committed
feat: Day 7: Camel Cards
1 parent 35b1a30 commit 8a41c5a

File tree

8 files changed

+1230
-4
lines changed

8 files changed

+1230
-4
lines changed

.github/badges/completion.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"schemaVersion": 1,
33
"label": "completion",
4-
"message": "12/50",
4+
"message": "14/50",
55
"color": "yellow"
66
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
1818
- [Day 4: Scratchcards](day-04-scratchcards/)
1919
- [Day 5: If You Give A Seed A Fertilizer](day-05-if-you-give-a-seed-a-fertilizer/)
2020
- [Day 6: Wait For It](day-06-wait-for-it/)
21-
- [Day 7: TBD](day-07/)
21+
- [Day 7: Camel Cards](day-07-camel-cards/)
2222
- [Day 8: TBD](day-08/)
2323
- [Day 9: TBD](day-09/)
2424
- [Day 10: TBD](day-10/)

day-03-gear-ratios/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Day 3: Gear Ratios
22

3-
You and the Elf eventually reach a [gondola lift](https://en.wikipedia.org/wiki/Gondola**lift) station; he says the gondola lift will take you up to the **water source**, but this is as far as he can bring you. You go inside.
3+
You and the Elf eventually reach a [gondola lift](https://en.wikipedia.org/wiki/Gondola_lift) station; he says the gondola lift will take you up to the **water source**, but this is as far as he can bring you. You go inside.
44

55
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
66

day-05-if-you-give-a-seed-a-fertilizer/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ You take the boat and find the gardener right where you were told he would be: m
44

55
"A water source? Island Island **is** the water source!" You point out that Snow Island isn't receiving any water.
66

7-
"Oh, we had to stop the water because we **ran out of sand** to [filter](https://en.wikipedia.org/wiki/Sand**filter) it with! Can't make snow with dirty water. Don't worry, I'm sure we'll get more sand soon; we only turned off the water a few days... weeks... oh no." His face sinks into a look of horrified realization.
7+
"Oh, we had to stop the water because we **ran out of sand** to [filter](https://en.wikipedia.org/wiki/Sand_filter) it with! Can't make snow with dirty water. Don't worry, I'm sure we'll get more sand soon; we only turned off the water a few days... weeks... oh no." His face sinks into a look of horrified realization.
88

99
"I've been so busy making sure everyone here has food that I completely forgot to check why we stopped getting more sand! There's a ferry leaving soon that is headed over in that direction - it's much faster than your boat. Could you please go check it out?"
1010

day-07-camel-cards/README.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Day 7: Camel Cards
2+
3+
Your all-expenses-paid trip turns out to be a one-way, five-minute ride in an [airship](https://en.wikipedia.org/wiki/Airship). (At least it's a **cool** airship!) It drops you off at the edge of a vast desert and descends back to Island Island.
4+
5+
"Did you bring the parts?"
6+
7+
You turn around to see an Elf completely covered in white clothing, wearing goggles, and riding a large [camel](https://en.wikipedia.org/wiki/Dromedary).
8+
9+
"Did you bring the parts?" she asks again, louder this time. You aren't sure what parts she's looking for; you're here to figure out why the sand stopped.
10+
11+
"The parts! For the sand, yes! Come with me; I will show you." She beckons you onto the camel.
12+
13+
After riding a bit across the sands of Desert Island, you can see what look like very large rocks covering half of the horizon. The Elf explains that the rocks are all along the part of Desert Island that is directly above Island Island, making it hard to even get there. Normally, they use big machines to move the rocks and filter the sand, but the machines have broken down because Desert Island recently stopped receiving the **parts** they need to fix the machines.
14+
15+
You've already assumed it'll be your job to figure out why the parts stopped when she asks if you can help. You agree automatically.
16+
17+
Because the journey will take a few days, she offers to teach you the game of **Camel Cards**. Camel Cards is sort of similar to [poker](https://en.wikipedia.org/wiki/List_of_poker_hands) except it's designed to be easier to play while riding a camel.
18+
19+
In Camel Cards, you get a list of **hands**, and your goal is to order them based on the **strength** of each hand. A hand consists of **five cards** labeled one of `A`, `K`, `Q`, `J`, `T`, `9`, `8`, `7`, `6`, `5`, `4`, `3`, or `2`. The relative strength of each card follows this order, where `A` is the highest and `2` is the lowest.
20+
21+
Every hand is exactly one **type**. From strongest to weakest, they are:
22+
23+
* **Five of a kind**, where all five cards have the same label: `AAAAA`
24+
* **Four of a kind**, where four cards have the same label and one card has a different label: `AA8AA`
25+
* **Full house**, where three cards have the same label, and the remaining two cards share a different label: `23332`
26+
* **Three of a kind**, where three cards have the same label, and the remaining two cards are each different from any other card in the hand: `TTT98`
27+
* **Two pair**, where two cards share one label, two other cards share a second label, and the remaining card has a third label: `23432`
28+
* **One pair**, where two cards share one label, and the other three cards have a different label from the pair and each other: `A23A4`
29+
* **High card**, where all cards' labels are distinct: `23456`
30+
31+
Hands are primarily ordered based on type; for example, every **full house** is stronger than any **three of a kind**.
32+
33+
If two hands have the same type, a second ordering rule takes effect. Start by comparing the **first card in each hand**. If these cards are different, the hand with the stronger first card is considered stronger. If the first card in each hand have the **same label**, however, then move on to considering the **second card in each hand**. If they differ, the hand with the higher second card wins; otherwise, continue with the third card in each hand, then the fourth, then the fifth.
34+
35+
So, `33332` and `2AAAA` are both **four of a kind** hands, but `33332` is stronger because its first card is stronger. Similarly, `77888` and `77788` are both a **full house**, but `77888` is stronger because its third card is stronger (and both hands have the same first and second card).
36+
37+
To play Camel Cards, you are given a list of hands and their corresponding **bid** (your puzzle input). For example:
38+
39+
32T3K 765
40+
T55J5 684
41+
KK677 28
42+
KTJJT 220
43+
QQQJA 483
44+
45+
46+
This example shows five hands; each hand is followed by its **bid** amount. Each hand wins an amount equal to its bid multiplied by its **rank**, where the weakest hand gets rank 1, the second-weakest hand gets rank 2, and so on up to the strongest hand. Because there are five hands in this example, the strongest hand will have rank 5 and its bid will be multiplied by 5.
47+
48+
So, the first step is to put the hands in order of strength:
49+
50+
* `32T3K` is the only **one pair** and the other hands are all a stronger type, so it gets rank **1**.
51+
* `KK677` and `KTJJT` are both **two pair**. Their first cards both have the same label, but the second card of `KK677` is stronger (`K` vs `T`), so `KTJJT` gets rank **2** and `KK677` gets rank **3**.
52+
* `T55J5` and `QQQJA` are both **three of a kind**. `QQQJA` has a stronger first card, so it gets rank **5** and `T55J5` gets rank **4**.
53+
54+
Now, you can determine the total winnings of this set of hands by adding up the result of multiplying each hand's bid with its rank (`765` \* 1 + `220` \* 2 + `28` \* 3 + `684` \* 4 + `483` \* 5). So the **total winnings** in this example are **`6440`**.
55+
56+
Find the rank of every hand in your set. **What are the total winnings?**
57+
58+
## Part Two
59+
60+
To make things a little more interesting, the Elf introduces one additional rule. Now, `J` cards are [jokers](https://en.wikipedia.org/wiki/Joker_(playing_card)) - wildcards that can act like whatever card would make the hand the strongest type possible.
61+
62+
To balance this, **`J` cards are now the weakest** individual cards, weaker even than `2`. The other cards stay in the same order: `A`, `K`, `Q`, `T`, `9`, `8`, `7`, `6`, `5`, `4`, `3`, `2`, `J`.
63+
64+
`J` cards can pretend to be whatever card is best for the purpose of determining hand type; for example, `QJJQ2` is now considered **four of a kind**. However, for the purpose of breaking ties between two hands of the same type, `J` is always treated as `J`, not the card it's pretending to be: `JKKK2` is weaker than `QQQQ2` because `J` is weaker than `Q`.
65+
66+
Now, the above example goes very differently:
67+
68+
32T3K 765
69+
T55J5 684
70+
KK677 28
71+
KTJJT 220
72+
QQQJA 483
73+
74+
75+
* `32T3K` is still the only **one pair**; it doesn't contain any jokers, so its strength doesn't increase.
76+
* `KK677` is now the only **two pair**, making it the second-weakest hand.
77+
* `T55J5`, `KTJJT`, and `QQQJA` are now all **four of a kind**! `T55J5` gets rank 3, `QQQJA` gets rank 4, and `KTJJT` gets rank 5.
78+
79+
With the new joker rule, the total winnings in this example are **`5905`**.
80+
81+
Using the new joker rule, find the rank of every hand in your set. **What are the new total winnings?**
82+
83+
## References
84+
- https://adventofcode.com/2023/day/7

day-07-camel-cards/part1.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module.exports = (input) => {
2+
const order = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'];
3+
4+
const isFiveOfAKind = (hand) => hand.some(([, x]) => x === 5);
5+
const isFourOfAKind = (hand) => hand.some(([, x]) => x === 4);
6+
const isFullHouse = (hand) => hand.some(([, x]) => x === 3) && hand.some(([, x]) => x === 2);
7+
const isThreeOfAKind = (hand) => hand.some(([, x]) => x === 3);
8+
const isTwoPair = (hand) => hand.filter(([, x]) => x === 2).length === 2;
9+
const isOnePair = (hand) => hand.some(([, x]) => x === 2);
10+
11+
const count = (cards) => cards.split('').reduce((hand, card) => ({
12+
...hand,
13+
[card]: (hand[card] || 0) + 1,
14+
}), {});
15+
16+
const items = input
17+
.split('\n')
18+
.map((line) => {
19+
const [cards, bid] = line.trim().split(' ');
20+
const counted = Object.entries(count(cards));
21+
22+
return {
23+
order: cards.split('').map((card) => order.indexOf(card)),
24+
type: isFiveOfAKind(counted) ? 6 :
25+
isFourOfAKind(counted) ? 5 :
26+
isFullHouse(counted) ? 4 :
27+
isThreeOfAKind(counted) ? 3 :
28+
isTwoPair(counted) ? 2 :
29+
isOnePair(counted) ? 1 : 0,
30+
bid: +bid,
31+
};
32+
});
33+
34+
return items
35+
.sort((a, b) => {
36+
return a.type !== b.type ? a.type - b.type :
37+
a.order[0] !== b.order[0] ? b.order[0] - a.order[0] :
38+
a.order[1] !== b.order[1] ? b.order[1] - a.order[1] :
39+
a.order[2] !== b.order[2] ? b.order[2] - a.order[2] :
40+
a.order[3] !== b.order[3] ? b.order[3] - a.order[3] :
41+
a.order[4] !== b.order[4] ? b.order[4] - a.order[4] : 0;
42+
})
43+
.reduce((sum, hand, i) => sum + (i + 1) * hand.bid, 0);
44+
};

day-07-camel-cards/part2.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module.exports = (input) => {
2+
const order = ['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J'];
3+
4+
const isFiveOfAKind = (hand) => {
5+
return hand.some(([, x]) => x === 5) ||
6+
hand.some(([, x]) => x + new Map(hand).get('J') === 5);
7+
};
8+
9+
const isFourOfAKind = (hand) => {
10+
return hand.some(([, x]) => x === 4) ||
11+
hand.some(([card, x]) => card !== 'J' && x + new Map(hand).get('J') === 4);
12+
};
13+
14+
const isFullHouse = (hand) => {
15+
return hand.some(([, x]) => x === 2) && hand.some(([, x]) => x === 3) ||
16+
hand.filter(([, x]) => x === 2).length === 2 && new Map(hand).get('J') === 1;
17+
};
18+
19+
const isThreeOfAKind = (hand) => {
20+
return hand.some(([, x]) => x === 3) ||
21+
hand.some(([card, x]) => card !== 'J' && x + new Map(hand).get('J') === 3);
22+
};
23+
24+
const isTwoPair = (hand) => hand.filter(([, x]) => x === 2).length === 2;
25+
26+
const isOnePair = (hand) => {
27+
return hand.some(([, x]) => x === 2) ||
28+
new Map(hand).get('J') === 1;
29+
};
30+
31+
const count = (hand) => hand.split('').reduce((hand, card) => ({
32+
...hand,
33+
[card]: (hand[card] || 0) + 1,
34+
}), {});
35+
36+
return input
37+
.split('\n')
38+
.map((line) => {
39+
const [cards, bid] = line.trim().split(' ');
40+
const counted = Object.entries(count(cards));
41+
42+
return {
43+
order: cards.split('').map((card) => order.indexOf(card)),
44+
type: isFiveOfAKind(counted) ? 6 :
45+
isFourOfAKind(counted) ? 5 :
46+
isFullHouse(counted) ? 4 :
47+
isThreeOfAKind(counted) ? 3 :
48+
isTwoPair(counted) ? 2 :
49+
isOnePair(counted) ? 1 : 0,
50+
bid: +bid,
51+
};
52+
})
53+
.sort((a, b) => {
54+
return a.type !== b.type ? a.type - b.type :
55+
a.order[0] !== b.order[0] ? b.order[0] - a.order[0] :
56+
a.order[1] !== b.order[1] ? b.order[1] - a.order[1] :
57+
a.order[2] !== b.order[2] ? b.order[2] - a.order[2] :
58+
a.order[3] !== b.order[3] ? b.order[3] - a.order[3] :
59+
a.order[4] !== b.order[4] ? b.order[4] - a.order[4] : 0;
60+
})
61+
.reduce((sum, hand, i) => sum + (i + 1) * hand.bid, 0);
62+
};

0 commit comments

Comments
 (0)