aoc/year2023/
day07.rs

1//! # Camel Cards
2//!
3//! The types of each hand are computed from the frequency of the cards ordered in descending order.
4//! For example, a full house would have 1 card with a frequency of 3 and a second with a
5//! frequency of 2, giving `[3, 2]`. Similarly, two pair would be `[2, 2, 1]`.
6//! Array comparisons will sort the hand types in order.
7//!
8//! To make comparisons faster the frequencies and the card ranks are packed into a `usize`:
9//!
10//! * `55222` => `0x3200055222`
11//! * `32T3K` => `0x2111032a3d`
12//!
13//! For part two we add the number of jokers to the highest frequency (which could already be
14//! jokers!).
15//!
16//! * `QQQJA` => `0x41000ccc1a`
17use crate::util::parse::*;
18use std::cmp::Reverse;
19
20pub struct Hand {
21    cards: [u8; 5],
22    bid: usize,
23}
24
25pub fn parse(input: &str) -> Vec<Hand> {
26    input
27        .lines()
28        .map(|line| {
29            let (prefix, suffix) = line.split_at(5);
30            let cards = prefix.as_bytes().try_into().unwrap();
31            let bid = suffix.unsigned();
32            Hand { cards, bid }
33        })
34        .collect()
35}
36
37pub fn part1(input: &[Hand]) -> usize {
38    sort(input, 11)
39}
40
41pub fn part2(input: &[Hand]) -> usize {
42    sort(input, 1)
43}
44
45fn sort(input: &[Hand], j: usize) -> usize {
46    let mut hands: Vec<_> = input
47        .iter()
48        .map(|&Hand { cards, bid }| {
49            let rank = cards.map(|b| match b {
50                b'A' => 14,
51                b'K' => 13,
52                b'Q' => 12,
53                b'J' => j,
54                b'T' => 10,
55                _ => b.to_decimal() as usize,
56            });
57
58            let mut freq = [0; 15];
59            for r in rank {
60                freq[r] += 1;
61            }
62
63            let jokers = freq[1];
64            freq[1] = 0;
65            freq.sort_unstable_by_key(|&card| Reverse(card));
66            freq[0] += jokers;
67
68            // To speed up comparisons, pack the frequencies and card ranks
69            // into the nibbles of a `usize`.
70            let key = freq[..5].iter().chain(&rank).fold(0, |key, &value| (key << 4) | value);
71
72            (key, bid)
73        })
74        .collect();
75
76    hands.sort_unstable();
77    hands.iter().enumerate().map(|(i, (_, bid))| (i + 1) * bid).sum()
78}