-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46ded34
commit 02973a5
Showing
6 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
|
||
--- Day 7: Camel Cards --- | ||
|
||
Your all-expenses-paid trip turns out to be a one-way, five-minute ride in an 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. | ||
|
||
"Did you bring the parts?" | ||
|
||
You turn around to see an Elf completely covered in white clothing, wearing goggles, and riding a large camel. | ||
|
||
"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. | ||
|
||
"The parts! For the sand, yes! Come with me; I will show you." She beckons you onto the camel. | ||
|
||
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. | ||
|
||
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. | ||
|
||
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 except it's designed to be easier to play while riding a camel. | ||
|
||
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. | ||
|
||
Every hand is exactly one type. From strongest to weakest, they are: | ||
|
||
Five of a kind, where all five cards have the same label: AAAAA | ||
Four of a kind, where four cards have the same label and one card has a different label: AA8AA | ||
Full house, where three cards have the same label, and the remaining two cards share a different label: 23332 | ||
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 | ||
Two pair, where two cards share one label, two other cards share a second label, and the remaining card has a third label: 23432 | ||
One pair, where two cards share one label, and the other three cards have a different label from the pair and each other: A23A4 | ||
High card, where all cards' labels are distinct: 23456 | ||
|
||
Hands are primarily ordered based on type; for example, every full house is stronger than any three of a kind. | ||
|
||
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. | ||
|
||
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). | ||
|
||
To play Camel Cards, you are given a list of hands and their corresponding bid (your puzzle input). For example: | ||
|
||
32T3K 765 | ||
T55J5 684 | ||
KK677 28 | ||
KTJJT 220 | ||
QQQJA 483 | ||
|
||
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. | ||
|
||
So, the first step is to put the hands in order of strength: | ||
|
||
32T3K is the only one pair and the other hands are all a stronger type, so it gets rank 1. | ||
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. | ||
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. | ||
|
||
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. | ||
|
||
Find the rank of every hand in your set. What are the total winnings? | ||
|
||
Your puzzle answer was 253910319. | ||
|
||
The first half of this puzzle is complete! It provides one gold star: * | ||
|
||
--- Part Two --- | ||
|
||
To make things a little more interesting, the Elf introduces one additional rule. Now, J cards are jokers - wildcards that can act like whatever card would make the hand the strongest type possible. | ||
|
||
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. | ||
|
||
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. | ||
|
||
Now, the above example goes very differently: | ||
|
||
32T3K 765 | ||
T55J5 684 | ||
KK677 28 | ||
KTJJT 220 | ||
QQQJA 483 | ||
|
||
32T3K is still the only one pair; it doesn't contain any jokers, so its strength doesn't increase. | ||
KK677 is now the only two pair, making it the second-weakest hand. | ||
T55J5, KTJJT, and QQQJA are now all four of a kind! T55J5 gets rank 3, QQQJA gets rank 4, and KTJJT gets rank 5. | ||
|
||
With the new joker rule, the total winnings in this example are 5905. | ||
|
||
Using the new joker rule, find the rank of every hand in your set. What are the new total winnings? | ||
|
||
Answer: | ||
|
||
Although it hasn't changed, you can still get your puzzle input. | ||
|
||
You can also [Shareon Twitter Mastodon] this puzzle. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <assert.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
|
||
#include "aoc/macros.h" | ||
#include "hand.h" | ||
|
||
#define LT -1 | ||
#define EQ 0 | ||
#define GT 1 | ||
|
||
// The higher the value the stronger the card. | ||
// Strength range is [0, 12]. | ||
static inline int card_strength(char c) { | ||
switch (c) { | ||
case 'T': return 8; | ||
case 'J': return 9; | ||
case 'Q': return 10; | ||
case 'K': return 11; | ||
case 'A': return 12; | ||
default: assert(c >= '2' && c <= '9'); return c - '2'; | ||
} | ||
} | ||
|
||
static inline int break_tie(Hand x, Hand y) { | ||
// 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. | ||
for (int i = 0; i < CARDS_PER_HAND; i++) { | ||
char lhs = x.cards[i], rhs = y.cards[i]; | ||
if (lhs != rhs) { | ||
return card_strength(lhs) < card_strength(rhs) ? LT : GT; | ||
} | ||
} | ||
return EQ; | ||
} | ||
|
||
hand_strength Hand_compute_strength(Hand h) { | ||
int occurences[13] = {0}; | ||
for (int i = 0; i < CARDS_PER_HAND; i++) { | ||
occurences[card_strength(h.cards[i])]++; | ||
} | ||
|
||
bool has_quintet = false, has_quadruple = false, has_triple = false; | ||
int pair_count = 0; | ||
|
||
for (size_t i = 0; i < ARRAY_LENGTH(occurences); i++) { | ||
if (occurences[i] == 5) { | ||
has_quintet = true; | ||
} else if (occurences[i] == 4) { | ||
has_quadruple = true; | ||
} else if (occurences[i] == 3) { | ||
has_triple = true; | ||
} else if (occurences[i] == 2) { | ||
pair_count++; | ||
} | ||
} | ||
|
||
if (has_quintet) return FIVE_KIND; | ||
if (has_quadruple) return FOUR_KIND; | ||
if (has_triple && pair_count > 0) return FULL_HOUSE; | ||
if (has_triple) return THREE_KIND; | ||
if (pair_count == 2) return TWO_PAIR; | ||
if (pair_count == 1) return ONE_PAIR; | ||
return HIGH_CARD; | ||
} | ||
|
||
// return GT if x > y | ||
static inline int Hand_compare(Hand x, Hand y) { | ||
|
||
hand_strength x_strength = Hand_compute_strength(x); | ||
hand_strength y_strength = Hand_compute_strength(y); | ||
|
||
if (x_strength < y_strength) { return LT; } | ||
if (x_strength > y_strength) { return GT; } | ||
assert(x_strength == y_strength); | ||
return break_tie(x, y); | ||
} | ||
|
||
#define SORT_NAME Hand | ||
#define SORT_TYPE Hand | ||
#define SORT_CMP(x, y) (Hand_compare(x, y)) | ||
#include "sort.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include "aoc/types.h" | ||
#include <stddef.h> | ||
|
||
#define CARDS_PER_HAND 5 | ||
|
||
typedef struct { | ||
char cards[CARDS_PER_HAND]; | ||
i32 bid; | ||
} Hand; | ||
|
||
typedef enum { | ||
HIGH_CARD, | ||
ONE_PAIR, | ||
TWO_PAIR, | ||
THREE_KIND, | ||
FULL_HOUSE, | ||
FOUR_KIND, | ||
FIVE_KIND | ||
} hand_strength; | ||
|
||
hand_strength Hand_compute_strength(Hand h); | ||
|
||
// forward decl | ||
void Hand_tim_sort(Hand *dst, const size_t size); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Author: Michael Adler | ||
* | ||
* Copyright: 2023 Michael Adler | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "solve.h" | ||
#include "aoc/all.h" | ||
|
||
#include "hand.h" | ||
|
||
#define MAX_HANDS 1000 | ||
|
||
void solve(const char *buf, size_t buf_size, Solution *result) { | ||
i64 part1 = 0, part2 = 0; | ||
size_t pos = 0; | ||
|
||
Hand hand[MAX_HANDS]; | ||
size_t hand_count = 0; | ||
|
||
while (pos < buf_size) { | ||
for (int i = 0; i < CARDS_PER_HAND; i++) { | ||
hand[hand_count].cards[i] = buf[pos++]; | ||
} | ||
hand[hand_count].bid = aoc_parse_nonnegative(buf, &pos); | ||
hand_count++; | ||
pos++; // newline | ||
} | ||
|
||
Hand_tim_sort(hand, hand_count); // weakest hand is first | ||
|
||
for (size_t i = 0; i < hand_count; i++) { | ||
log_debug("%.*s %d", 5, hand[i].cards, Hand_compute_strength(hand[i])); | ||
// Each hand wins an amount equal to its bid multiplied by its rank | ||
i64 rank = i + 1, bid = hand[i].bid; | ||
part1 += rank * bid; | ||
} | ||
|
||
aoc_itoa(part1, result->part1, 10); | ||
aoc_itoa(part2, result->part2, 10); | ||
} | ||
|
||
int solve_input(const char *fname, Solution *result) { | ||
char buf[1 << 14]; | ||
int n = aoc_io_read_input(fname, buf, sizeof(buf)); | ||
if (n <= 0) { | ||
fprintf(stderr, "Failed to read %s\n", fname); | ||
return -1; | ||
} | ||
solve(buf, n, result); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Author: Michael Adler | ||
* | ||
* Copyright: 2023 Michael Adler | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#define CTEST_MAIN | ||
|
||
#include "ctest.h" | ||
#include "solve.h" | ||
|
||
CTEST(day07, example) { | ||
const char *buf = "32T3K 765\n\ | ||
T55J5 684\n\ | ||
KK677 28\n\ | ||
KTJJT 220\n\ | ||
QQQJA 483\n"; | ||
Solution solution; | ||
solve(buf, strlen(buf), &solution); | ||
ASSERT_STR("6440", solution.part1); | ||
ASSERT_STR("0", solution.part2); | ||
} | ||
|
||
#ifdef HAVE_INPUTS | ||
CTEST(day07, real) { | ||
Solution solution; | ||
solve_input("input/" DAY ".txt", &solution); | ||
ASSERT_STR("253910319", solution.part1); | ||
// ASSERT_STR("0", solution.part2); | ||
} | ||
#endif | ||
|
||
int main(int argc, const char *argv[]) { return ctest_main(argc, argv); } |