minor optimimization for sorting in day 07.

This commit is contained in:
Gabe Venberg 2023-12-07 15:42:21 -06:00
parent 5a94ee9c33
commit 7f9256db4e
3 changed files with 273 additions and 531 deletions

View file

@ -19,7 +19,7 @@ pub enum Card {
Joker = 1, Joker = 1,
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Formation { pub enum Formation {
FiveOfKind = 7, FiveOfKind = 7,
FourOfKind = 6, FourOfKind = 6,
@ -125,17 +125,24 @@ impl UnorderedHand {
} }
} }
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq)]
pub struct Hand { pub struct Hand {
pub cards: [Card; 5], pub cards: [Card; 5],
unordered_hand: UnorderedHand,
} }
impl Hand { impl Hand {
pub fn determine_formation(&self) -> Formation { pub fn new(cards: [Card; 5]) -> Hand {
self.get_unordered_hand().determine_formation() Hand {
unordered_hand: Self::get_unordered_hand(&cards),
cards,
} }
fn get_unordered_hand(&self) -> UnorderedHand { }
self.cards pub fn determine_formation(&self) -> Formation {
self.unordered_hand.determine_formation()
}
fn get_unordered_hand(cards: &[Card]) -> UnorderedHand {
cards
.iter() .iter()
.fold(UnorderedHand::default(), |mut acc, card| { .fold(UnorderedHand::default(), |mut acc, card| {
match card { match card {
@ -158,11 +165,10 @@ impl Hand {
}) })
} }
pub fn turn_jacks_to_jokers(&self) -> Hand { pub fn turn_jacks_to_jokers(&self) -> Hand {
Hand { Hand::new(
cards: self self.cards
.cards
.map(|c| if c == Card::Jack { Card::Joker } else { c }), .map(|c| if c == Card::Jack { Card::Joker } else { c }),
} )
} }
} }
@ -213,9 +219,7 @@ pub fn parse(input: &str) -> Vec<(Hand, u32)> {
e => panic!("invalid card {}", e), e => panic!("invalid card {}", e),
}) })
.collect(); .collect();
let hand = Hand { let hand = Hand::new(hand.try_into().unwrap());
cards: hand.try_into().unwrap(),
};
(hand, bid) (hand, bid)
}) })
.collect() .collect()
@ -229,147 +233,109 @@ mod tests {
fn test_ord_with_joker_conversion() { fn test_ord_with_joker_conversion() {
let mut input = vec![ let mut input = vec![
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace],
},
1, 1,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::King, Card::Joker, Card::Joker]),
cards: [Card::Queen, Card::Two, Card::King, Card::Joker, Card::Joker],
},
13, 13,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]),
cards: [Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen],
},
19, 19,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Joker]),
cards: [Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Joker],
},
17, 17,
), ),
( (
Hand { Hand::new([
cards: [
Card::Tim, Card::Tim,
Card::Three, Card::Three,
Card::Queen, Card::Queen,
Card::Three, Card::Three,
Card::Three, Card::Three,
], ]),
},
11, 11,
), ),
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Joker]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Joker],
},
3, 3,
), ),
( (
Hand { Hand::new([Card::Joker, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Joker, Card::Three, Card::Four, Card::Five, Card::Ace],
},
2, 2,
), ),
( (
Hand { Hand::new([Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]),
cards: [Card::Three, Card::Two, Card::Tim, Card::Three, Card::King],
},
5, 5,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Five, Card::Five, Card::Joker, Card::Five]),
cards: [Card::Tim, Card::Five, Card::Five, Card::Joker, Card::Five],
},
29, 29,
), ),
( (
Hand { Hand::new([Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]),
cards: [Card::King, Card::King, Card::Six, Card::Seven, Card::Seven],
},
7, 7,
), ),
( (
Hand { Hand::new([Card::King, Card::Tim, Card::Joker, Card::Joker, Card::Tim]),
cards: [Card::King, Card::Tim, Card::Joker, Card::Joker, Card::Tim],
},
34, 34,
), ),
( (
Hand { Hand::new([
cards: [
Card::Queen, Card::Queen,
Card::Queen, Card::Queen,
Card::Queen, Card::Queen,
Card::Joker, Card::Joker,
Card::Ace, Card::Ace,
], ]),
},
31, 31,
), ),
( (
Hand { Hand::new([
cards: [
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
], ]),
},
37, 37,
), ),
( (
Hand { Hand::new([Card::Joker, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Joker, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
43, 43,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Joker]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Joker],
},
59, 59,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
61, 61,
), ),
( (
Hand { Hand::new([Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
23, 23,
), ),
( (
Hand { Hand::new([
cards: [
Card::Two, Card::Two,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
], ]),
},
53, 53,
), ),
( (
Hand { Hand::new([
cards: [
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Two, Card::Two,
], ]),
},
41, 41,
), ),
]; ];
@ -377,7 +343,7 @@ mod tests {
.into_iter() .into_iter()
.map(|c| (c.0.turn_jacks_to_jokers(), c.1)) .map(|c| (c.0.turn_jacks_to_jokers(), c.1))
.collect(); .collect();
input.sort_by_key(|set| set.0); input.sort_by(|a, b| a.0.cmp(&b.0));
println!("{:#?}", input); println!("{:#?}", input);
//check that the bids are sorted (the input is curated to ensure that the bids are sorted //check that the bids are sorted (the input is curated to ensure that the bids are sorted
//when the hands are sorted.) //when the hands are sorted.)
@ -388,147 +354,109 @@ mod tests {
fn test_determine_formation_after_joker_conversion() { fn test_determine_formation_after_joker_conversion() {
let mut tests = vec![ let mut tests = vec![
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace],
},
Formation::HighCard, Formation::HighCard,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::King, Card::Joker, Card::Joker]),
cards: [Card::Queen, Card::Two, Card::King, Card::Joker, Card::Joker],
},
Formation::ThreeOfKind, Formation::ThreeOfKind,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]),
cards: [Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen],
},
Formation::FullHouse, Formation::FullHouse,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Joker]),
cards: [Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Joker],
},
Formation::FullHouse, Formation::FullHouse,
), ),
( (
Hand { Hand::new([
cards: [
Card::Tim, Card::Tim,
Card::Three, Card::Three,
Card::Queen, Card::Queen,
Card::Three, Card::Three,
Card::Three, Card::Three,
], ]),
},
Formation::ThreeOfKind, Formation::ThreeOfKind,
), ),
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Joker]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Joker],
},
Formation::OnePair, Formation::OnePair,
), ),
( (
Hand { Hand::new([Card::Joker, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Joker, Card::Three, Card::Four, Card::Five, Card::Ace],
},
Formation::OnePair, Formation::OnePair,
), ),
( (
Hand { Hand::new([Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]),
cards: [Card::Three, Card::Two, Card::Tim, Card::Three, Card::King],
},
Formation::OnePair, Formation::OnePair,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Five, Card::Five, Card::Joker, Card::Five]),
cards: [Card::Tim, Card::Five, Card::Five, Card::Joker, Card::Five],
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
( (
Hand { Hand::new([Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]),
cards: [Card::King, Card::King, Card::Six, Card::Seven, Card::Seven],
},
Formation::TwoPair, Formation::TwoPair,
), ),
( (
Hand { Hand::new([Card::King, Card::Tim, Card::Joker, Card::Joker, Card::Tim]),
cards: [Card::King, Card::Tim, Card::Joker, Card::Joker, Card::Tim],
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
( (
Hand { Hand::new([
cards: [
Card::Queen, Card::Queen,
Card::Queen, Card::Queen,
Card::Queen, Card::Queen,
Card::Joker, Card::Joker,
Card::Ace, Card::Ace,
], ]),
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
( (
Hand { Hand::new([
cards: [
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
], ]),
},
Formation::FiveOfKind, Formation::FiveOfKind,
), ),
( (
Hand { Hand::new([Card::Joker, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Joker, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
Formation::FiveOfKind, Formation::FiveOfKind,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Joker]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Joker],
},
Formation::FiveOfKind, Formation::FiveOfKind,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
Formation::FiveOfKind, Formation::FiveOfKind,
), ),
( (
Hand { Hand::new([Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
( (
Hand { Hand::new([
cards: [
Card::Two, Card::Two,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
], ]),
},
Formation::FiveOfKind, Formation::FiveOfKind,
), ),
( (
Hand { Hand::new([
cards: [
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Two, Card::Two,
], ]),
},
Formation::FiveOfKind, Formation::FiveOfKind,
), ),
]; ];
@ -545,127 +473,89 @@ mod tests {
fn test_ord() { fn test_ord() {
let mut input = vec![ let mut input = vec![
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace],
},
1, 1,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack]),
cards: [Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack],
},
13, 13,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]),
cards: [Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen],
},
19, 19,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack]),
cards: [Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack],
},
17, 17,
), ),
( (
Hand { Hand::new([
cards: [
Card::Tim, Card::Tim,
Card::Three, Card::Three,
Card::Queen, Card::Queen,
Card::Three, Card::Three,
Card::Three, Card::Three,
], ]),
},
11, 11,
), ),
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack],
},
3, 3,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace],
},
2, 2,
), ),
( (
Hand { Hand::new([Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]),
cards: [Card::Three, Card::Two, Card::Tim, Card::Three, Card::King],
},
5, 5,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five]),
cards: [Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five],
},
29, 29,
), ),
( (
Hand { Hand::new([Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]),
cards: [Card::King, Card::King, Card::Six, Card::Seven, Card::Seven],
},
7, 7,
), ),
( (
Hand { Hand::new([Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim]),
cards: [Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim],
},
34, 34,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace]),
cards: [Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace],
},
31, 31,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
cards: [Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack],
},
37, 37,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
43, 43,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack],
},
59, 59,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
61, 61,
), ),
( (
Hand { Hand::new([Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
23, 23,
), ),
( (
Hand { Hand::new([Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
cards: [Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack],
},
53, 53,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two]),
cards: [Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two],
},
41, 41,
), ),
]; ];
input.sort_by_key(|set| set.0); input.sort_by(|a, b| a.0.cmp(&b.0));
println!("{:#?}", input); println!("{:#?}", input);
assert_eq!( assert_eq!(
input.iter().map(|h| h.1).collect::<Vec<i32>>(), input.iter().map(|h| h.1).collect::<Vec<i32>>(),
@ -677,123 +567,85 @@ mod tests {
fn test_determine_formation() { fn test_determine_formation() {
let tests = vec![ let tests = vec![
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace],
},
Formation::HighCard, Formation::HighCard,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack]),
cards: [Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack],
},
Formation::OnePair, Formation::OnePair,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]),
cards: [Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen],
},
Formation::FullHouse, Formation::FullHouse,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack]),
cards: [Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack],
},
Formation::TwoPair, Formation::TwoPair,
), ),
( (
Hand { Hand::new([
cards: [
Card::Tim, Card::Tim,
Card::Three, Card::Three,
Card::Queen, Card::Queen,
Card::Three, Card::Three,
Card::Three, Card::Three,
], ]),
},
Formation::ThreeOfKind, Formation::ThreeOfKind,
), ),
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack],
},
Formation::HighCard, Formation::HighCard,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace],
},
Formation::HighCard, Formation::HighCard,
), ),
( (
Hand { Hand::new([Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]),
cards: [Card::Three, Card::Two, Card::Tim, Card::Three, Card::King],
},
Formation::OnePair, Formation::OnePair,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five]),
cards: [Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five],
},
Formation::ThreeOfKind, Formation::ThreeOfKind,
), ),
( (
Hand { Hand::new([Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]),
cards: [Card::King, Card::King, Card::Six, Card::Seven, Card::Seven],
},
Formation::TwoPair, Formation::TwoPair,
), ),
( (
Hand { Hand::new([Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim]),
cards: [Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim],
},
Formation::TwoPair, Formation::TwoPair,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace]),
cards: [Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace],
},
Formation::ThreeOfKind, Formation::ThreeOfKind,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
cards: [Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack],
},
Formation::FiveOfKind, Formation::FiveOfKind,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack],
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
Formation::FiveOfKind, Formation::FiveOfKind,
), ),
( (
Hand { Hand::new([Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
( (
Hand { Hand::new([Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
cards: [Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack],
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two]),
cards: [Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two],
},
Formation::FourOfKind, Formation::FourOfKind,
), ),
]; ];
@ -829,123 +681,85 @@ mod tests {
parse(input), parse(input),
vec![ vec![
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]
},
1 1
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack]),
cards: [Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack]
},
13 13
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]),
cards: [Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]
},
19 19
), ),
( (
Hand { Hand::new([Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack]),
cards: [Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack]
},
17 17
), ),
( (
Hand { Hand::new([
cards: [
Card::Tim, Card::Tim,
Card::Three, Card::Three,
Card::Queen, Card::Queen,
Card::Three, Card::Three,
Card::Three Card::Three
] ]),
},
11 11
), ),
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack]
},
3 3
), ),
( (
Hand { Hand::new([Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace]
},
2 2
), ),
( (
Hand { Hand::new([Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]),
cards: [Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]
},
5 5
), ),
( (
Hand { Hand::new([Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five]),
cards: [Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five]
},
29 29
), ),
( (
Hand { Hand::new([Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]),
cards: [Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]
},
7 7
), ),
( (
Hand { Hand::new([Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim]),
cards: [Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim]
},
34 34
), ),
( (
Hand { Hand::new([Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace]),
cards: [Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace]
},
31 31
), ),
( (
Hand { Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
cards: [Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack]
},
37 37
), ),
( (
Hand { Hand::new([Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace]
},
43 43
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack]
},
59 59
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]
},
61 61
), ),
( (
Hand { Hand::new([Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]
},
23 23
), ),
( (
Hand { Hand::new([Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
cards: [Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack]
},
53 53
), ),
( (
Hand { Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two]),
cards: [Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two]
},
41 41
) )
] ]

View file

@ -1,8 +1,12 @@
use crate::parse::*; use crate::parse::*;
pub fn part1(input: &mut [(Hand, u32)]) -> usize { pub fn part1(input: &mut [(Hand, u32)]) -> usize {
input.sort_by_key(|set| set.0); input.sort_by(|a, b| a.0.cmp(&b.0) );
input.iter().enumerate().map(|i| (i.0+1) * i.1.1 as usize).sum() input
.iter()
.enumerate()
.map(|i| (i.0 + 1) * i.1 .1 as usize)
.sum()
} }
#[cfg(test)] #[cfg(test)]
@ -13,123 +17,85 @@ mod tests {
fn test_part1() { fn test_part1() {
let mut input = vec![ let mut input = vec![
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace],
},
1, 1,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack]),
cards: [Card::Queen, Card::Two, Card::King, Card::Jack, Card::Jack],
},
13, 13,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]),
cards: [Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen],
},
19, 19,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack]),
cards: [Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Jack],
},
17, 17,
), ),
( (
Hand { Hand::new([
cards: [
Card::Tim, Card::Tim,
Card::Three, Card::Three,
Card::Queen, Card::Queen,
Card::Three, Card::Three,
Card::Three, Card::Three,
], ]),
},
11, 11,
), ),
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Jack],
},
3, 3,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Jack, Card::Three, Card::Four, Card::Five, Card::Ace],
},
2, 2,
), ),
( (
Hand { Hand::new([Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]),
cards: [Card::Three, Card::Two, Card::Tim, Card::Three, Card::King],
},
5, 5,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five]),
cards: [Card::Tim, Card::Five, Card::Five, Card::Jack, Card::Five],
},
29, 29,
), ),
( (
Hand { Hand::new([Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]),
cards: [Card::King, Card::King, Card::Six, Card::Seven, Card::Seven],
},
7, 7,
), ),
( (
Hand { Hand::new([Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim]),
cards: [Card::King, Card::Tim, Card::Jack, Card::Jack, Card::Tim],
},
34, 34,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace]),
cards: [Card::Queen, Card::Queen, Card::Queen, Card::Jack, Card::Ace],
},
31, 31,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
cards: [Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Jack],
},
37, 37,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Jack, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
43, 43,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Jack],
},
59, 59,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
61, 61,
), ),
( (
Hand { Hand::new([Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
23, 23,
), ),
( (
Hand { Hand::new([Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack]),
cards: [Card::Two, Card::Jack, Card::Jack, Card::Jack, Card::Jack],
},
53, 53,
), ),
( (
Hand { Hand::new([Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two]),
cards: [Card::Jack, Card::Jack, Card::Jack, Card::Jack, Card::Two],
},
41, 41,
), ),
]; ];

View file

@ -5,7 +5,7 @@ pub fn part2(input: &[(Hand, u32)]) -> usize {
.iter() .iter()
.map(|c| (c.0.turn_jacks_to_jokers(), c.1)) .map(|c| (c.0.turn_jacks_to_jokers(), c.1))
.collect(); .collect();
input.sort_by_key(|set| set.0); input.sort_by(|a, b| a.0.cmp(&b.0) );
input input
.iter() .iter()
.enumerate() .enumerate()
@ -21,147 +21,109 @@ mod tests {
fn test_part2() { fn test_part2() {
let input = vec![ let input = vec![
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Ace],
},
1, 1,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::King, Card::Joker, Card::Joker]),
cards: [Card::Queen, Card::Two, Card::King, Card::Joker, Card::Joker],
},
13, 13,
), ),
( (
Hand { Hand::new([Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen]),
cards: [Card::Queen, Card::Two, Card::Queen, Card::Two, Card::Queen],
},
19, 19,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Joker]),
cards: [Card::Tim, Card::Three, Card::Tim, Card::Three, Card::Joker],
},
17, 17,
), ),
( (
Hand { Hand::new([
cards: [
Card::Tim, Card::Tim,
Card::Three, Card::Three,
Card::Queen, Card::Queen,
Card::Three, Card::Three,
Card::Three, Card::Three,
], ]),
},
11, 11,
), ),
( (
Hand { Hand::new([Card::Two, Card::Three, Card::Four, Card::Five, Card::Joker]),
cards: [Card::Two, Card::Three, Card::Four, Card::Five, Card::Joker],
},
3, 3,
), ),
( (
Hand { Hand::new([Card::Joker, Card::Three, Card::Four, Card::Five, Card::Ace]),
cards: [Card::Joker, Card::Three, Card::Four, Card::Five, Card::Ace],
},
2, 2,
), ),
( (
Hand { Hand::new([Card::Three, Card::Two, Card::Tim, Card::Three, Card::King]),
cards: [Card::Three, Card::Two, Card::Tim, Card::Three, Card::King],
},
5, 5,
), ),
( (
Hand { Hand::new([Card::Tim, Card::Five, Card::Five, Card::Joker, Card::Five]),
cards: [Card::Tim, Card::Five, Card::Five, Card::Joker, Card::Five],
},
29, 29,
), ),
( (
Hand { Hand::new([Card::King, Card::King, Card::Six, Card::Seven, Card::Seven]),
cards: [Card::King, Card::King, Card::Six, Card::Seven, Card::Seven],
},
7, 7,
), ),
( (
Hand { Hand::new([Card::King, Card::Tim, Card::Joker, Card::Joker, Card::Tim]),
cards: [Card::King, Card::Tim, Card::Joker, Card::Joker, Card::Tim],
},
34, 34,
), ),
( (
Hand { Hand::new([
cards: [
Card::Queen, Card::Queen,
Card::Queen, Card::Queen,
Card::Queen, Card::Queen,
Card::Joker, Card::Joker,
Card::Ace, Card::Ace,
], ]),
},
31, 31,
), ),
( (
Hand { Hand::new([
cards: [
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
], ]),
},
37, 37,
), ),
( (
Hand { Hand::new([Card::Joker, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Joker, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
43, 43,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Joker]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Joker],
},
59, 59,
), ),
( (
Hand { Hand::new([Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Ace, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
61, 61,
), ),
( (
Hand { Hand::new([Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace]),
cards: [Card::Two, Card::Ace, Card::Ace, Card::Ace, Card::Ace],
},
23, 23,
), ),
( (
Hand { Hand::new([
cards: [
Card::Two, Card::Two,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
], ]),
},
53, 53,
), ),
( (
Hand { Hand::new([
cards: [
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Joker, Card::Joker,
Card::Two, Card::Two,
], ]),
},
41, 41,
), ),
]; ];