192 lines
4.3 KiB
Rust
192 lines
4.3 KiB
Rust
use crate::HandType::{
|
|
FiveOfAKind, FourOfAKind, FullHouse, HighCard, OnePair, ThreeOfAKind, TwoPair,
|
|
};
|
|
use std::cmp::Ordering;
|
|
use std::collections::HashMap;
|
|
|
|
const FILE_STR: &str = include_str!("input.txt");
|
|
|
|
#[derive(Clone, Debug, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
|
#[repr(u8)]
|
|
enum CardType {
|
|
#[default]
|
|
Two = 0,
|
|
Three,
|
|
Four,
|
|
Five,
|
|
Six,
|
|
Seven,
|
|
Eight,
|
|
Nine,
|
|
Joker,
|
|
Jack,
|
|
Queen,
|
|
King,
|
|
Ace,
|
|
}
|
|
|
|
impl From<char> for CardType {
|
|
fn from(value: char) -> Self {
|
|
use CardType::*;
|
|
|
|
match value {
|
|
'2' => Two,
|
|
'3' => Three,
|
|
'4' => Four,
|
|
'5' => Five,
|
|
'6' => Six,
|
|
'7' => Seven,
|
|
'8' => Eight,
|
|
'9' => Nine,
|
|
'T' => Joker,
|
|
'J' => Jack,
|
|
'Q' => Queen,
|
|
'K' => King,
|
|
'A' => Ace,
|
|
_ => panic!("Invalid card character"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default, PartialEq, Eq)]
|
|
struct Hand {
|
|
cards: [CardType; 5],
|
|
kind: HandType,
|
|
bet: usize,
|
|
}
|
|
|
|
impl Hand {
|
|
fn parse(line: &str) -> Self {
|
|
let mut parts = line.split_whitespace();
|
|
let raw_hand = parts.next().unwrap();
|
|
let raw_bet = parts.next().unwrap();
|
|
let cards: [CardType; 5] = raw_hand
|
|
.trim()
|
|
.chars()
|
|
.map(CardType::from)
|
|
.collect::<Vec<CardType>>()
|
|
.try_into()
|
|
.unwrap();
|
|
let bet = raw_bet.trim().parse::<usize>().unwrap();
|
|
let kind = HandType::from(cards.clone());
|
|
|
|
Hand { cards, kind, bet }
|
|
}
|
|
}
|
|
|
|
impl PartialOrd for Hand {
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
Some(self.cmp(other))
|
|
}
|
|
}
|
|
|
|
impl Ord for Hand {
|
|
/// Implement the sorting for each hand, first comparing the type of
|
|
/// hand, and then comparing the individual cards, if necessary
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
if self.kind != other.kind {
|
|
return self.kind.cmp(&other.kind);
|
|
}
|
|
|
|
for (i, card) in self.cards.iter().enumerate() {
|
|
if card != &other.cards[i] {
|
|
return card.cmp(&other.cards[i]);
|
|
}
|
|
}
|
|
|
|
Ordering::Equal
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default, PartialOrd, Ord, PartialEq, Eq, Copy, Clone)]
|
|
#[repr(u8)]
|
|
enum HandType {
|
|
#[default]
|
|
HighCard = 0,
|
|
OnePair,
|
|
TwoPair,
|
|
ThreeOfAKind,
|
|
FullHouse,
|
|
FourOfAKind,
|
|
FiveOfAKind,
|
|
}
|
|
|
|
impl From<[CardType; 5]> for HandType {
|
|
fn from(value: [CardType; 5]) -> Self {
|
|
let mut cards = Vec::from(value);
|
|
cards.sort_unstable();
|
|
|
|
let mut count_map: HashMap<CardType, usize> = HashMap::new();
|
|
cards.into_iter().for_each(|ct| {
|
|
match count_map.get(&ct) {
|
|
Some(count) => count_map.insert(ct, *count + 1),
|
|
None => count_map.insert(ct, 1),
|
|
};
|
|
});
|
|
let mut card_type_counts: Vec<usize> = count_map.into_values().collect();
|
|
card_type_counts.sort_unstable();
|
|
|
|
match card_type_counts[..] {
|
|
[5] => FiveOfAKind,
|
|
[1, 4] => FourOfAKind,
|
|
[2, 3] => FullHouse,
|
|
[1, 1, 3] => ThreeOfAKind,
|
|
[1, 2, 2] => TwoPair,
|
|
[1, 1, 1, 2] => OnePair,
|
|
_ => HighCard,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default)]
|
|
struct Game {
|
|
hands: Vec<Hand>,
|
|
}
|
|
|
|
impl Game {
|
|
fn parse(input: &str) -> Self {
|
|
let hands = input
|
|
.split('\n')
|
|
.filter(|s| !s.is_empty())
|
|
.map(Hand::parse)
|
|
.collect::<Vec<Hand>>();
|
|
|
|
Game { hands }
|
|
}
|
|
|
|
fn get_score_sum(&mut self) -> usize {
|
|
// Rank the hands
|
|
self.hands.sort_unstable();
|
|
|
|
self.hands.iter().enumerate().fold(0, |prev, curr| {
|
|
let (i, hand) = curr;
|
|
let rank = i + 1;
|
|
|
|
prev + (rank * hand.bet)
|
|
})
|
|
}
|
|
}
|
|
|
|
fn part_one() {
|
|
let mut game = Game::parse(FILE_STR);
|
|
let score = game.get_score_sum();
|
|
|
|
println!("Part 1: Sum of scores: {}", score);
|
|
}
|
|
|
|
fn main() {
|
|
part_one();
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
const EXAMPLE_FILE_STR: &str = include_str!("example-input.txt");
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn get_score_sum() {
|
|
let mut game = Game::parse(EXAMPLE_FILE_STR);
|
|
assert_eq!(6440, game.get_score_sum());
|
|
}
|
|
}
|