Part 1
Today's task is to rank hands in a Camel Cards game and compute the total score.
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?
#!/usr/bin/env bash
#
# Solver for https://adventofcode.com/2023/day/7 part 1
# Redirect the input file to this script, for example day7part1.sh < path/to/input.txt
#
set -euo pipefail
is_high() { [ $# = 5 ]; }
is_one_pair() { [ $# = 4 ]; }
is_two_pair() {
[ $# = 3 ] || return
for count; do
((count != 3)) || return 1
done
}
is_three_of_a_kind() {
[ $# = 3 ] || return
for count; do
((count == 3)) && return
done
return 1
}
is_full_house() {
[ $# = 2 ] || return
(($1 == 3 || $2 == 3))
}
is_four_of_a_kind() {
[ $# = 2 ] || return
(($1 == 4 || $2 == 4))
}
is_five_of_a_kind() { [ $# = 1 ]; }
solve_2023_day7_part1() {
local strengths=(
is_high
is_one_pair
is_two_pair
is_three_of_a_kind
is_full_house
is_four_of_a_kind
is_five_of_a_kind
)
local -A card_counts
local hand bid strength i c fun
while read -r hand bid; do
card_counts=()
for ((i = 0; i < ${#hand}; ++i)); do
c=${hand:i:1}
((card_counts[$c] = ${card_counts[$c]:-0} + 1))
done
strength=0
for fun in "${strengths[@]}"; do
# Disable warning, the values are internal and safe.
# shellcheck disable=SC2206
"$fun" "${card_counts[@]}" && break
((++strength))
done
echo "$strength $hand $bid"
done |
tr TJQKA abcde |
sort |
awk '{ sum += NR * $3 } END { print sum }'
}
solve_2023_day7_part1
Part 2
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.
#!/usr/bin/env bash
#
# Solver for https://adventofcode.com/2023/day/7 part 2
# Redirect the input file to this script, for example day7part2.sh < path/to/input.txt
#
set -euo pipefail
__HIGH_CARD=1
__ONE_PAIR=2
__TWO_PAIR=3
__THREE_OF_A_KIND=4
__FULL_HOUSE=5
__FOUR_OF_A_KIND=6
__FIVE_OF_A_KIND=7
global_strength=0
is_high() {
[ $# = 5 ] && global_strength=$__HIGH_CARD
}
is_one_pair() {
[ $# = 4 ] && global_strength=$__ONE_PAIR
}
is_two_pair() {
[ $# = 3 ] || return
for count; do
((count != 3)) || return
done
global_strength=$__TWO_PAIR
}
is_three_of_a_kind() {
[ $# = 3 ] || return
for count; do
((count == 3)) && global_strength=$__THREE_OF_A_KIND && return
done
return 1
}
is_full_house() {
[ $# = 2 ] || return
(($1 == 3 || $2 == 3)) && global_strength=$__FULL_HOUSE
}
is_four_of_a_kind() {
[ $# = 2 ] || return
(($1 == 4 || $2 == 4)) && global_strength=$__FOUR_OF_A_KIND
}
is_five_of_a_kind() {
[ $# = 1 ] && global_strength=$__FIVE_OF_A_KIND
}
strengths=(
is_high
is_one_pair
is_two_pair
is_three_of_a_kind
is_full_house
is_four_of_a_kind
is_five_of_a_kind
)
# Computes stregth of a hand and stores in the global variable $global_strength.
compute_strength() {
local hand=$1 i c
local -A card_counts
for ((i = 0; i < ${#hand}; ++i)); do
c=${hand:i:1}
((card_counts[$c] = ${card_counts[$c]:-0} + 1))
done
for fun in "${strengths[@]}"; do
# Disable warning, the values are internal and safe.
# shellcheck disable=SC2206
"$fun" "${card_counts[@]}" && return
done
}
illegal_hand_and_strength() {
local hand=$1
local strength=$2
echo >&2 "Illegal state: hand=$hand strength=$strength"
exit 1
}
solve_2023_day7_part2() {
local hand bid strength rest
while read -r hand bid; do
compute_strength "$hand"
strength=$global_strength
if [[ $hand =~ J ]]; then
case ${hand//[^J]} in
JJJJ*) strength=$__FIVE_OF_A_KIND ;;
JJJ)
rest=${hand//J}
if [ "${rest:0:1}" = "${rest:1:1}" ]; then
strength=$__FIVE_OF_A_KIND
else
strength=$__FOUR_OF_A_KIND
fi
;;
JJ)
case $strength in
"$__ONE_PAIR") strength=$__THREE_OF_A_KIND ;;
"$__TWO_PAIR") strength=$__FOUR_OF_A_KIND ;;
"$__FULL_HOUSE") strength=$__FIVE_OF_A_KIND ;;
*) illegal_hand_and_strength "$hand" "$strength" ;;
esac
;;
J)
case $strength in
"$__HIGH_CARD") strength=$__ONE_PAIR ;;
"$__ONE_PAIR") strength=$__THREE_OF_A_KIND ;;
"$__TWO_PAIR") strength=$__FULL_HOUSE ;;
"$__THREE_OF_A_KIND") strength=$__FOUR_OF_A_KIND ;;
"$__FOUR_OF_A_KIND") strength=$__FIVE_OF_A_KIND ;;
*) illegal_hand_and_strength "$hand" "$strength" ;;
esac
;;
*) illegal_hand_and_strength "$hand" "$strength" ;;
esac
fi
echo "$strength ${hand//J/0} $bid"
done |
tr TQKA acde |
sort |
awk '{ sum += NR * $3 } END { print sum }'
}
solve_2023_day7_part2
Review request
I know this is a bit whimsical, and Bash is a poor choice to solve algorithmic puzzles. Also, this code is intended as a one-off, and not for reuse. I'm solving in Bash because, and as long as, it gives me joy. My main goals are:
- Compute the correct solution to the full input within seconds.
- Use idiomatic Bash.
- Easy to read and understand.
Do you see any patterns here that you would replace with better patterns?
Do you see a simpler way to solve any part of the puzzle with Bash and common shell tools?
What would you do differently?