summaryrefslogtreecommitdiff
path: root/2023/Rust/src
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-12-07 14:28:59 +0200
committeromagdy7 <omar.professional8777@gmail.com>2023-12-07 14:28:59 +0200
commit88c1d71d4148b8eb45e8b06c6f80d3014cc5a3da (patch)
tree35e6b8d987b62bba4731174cc9e0ba71eeaf7564 /2023/Rust/src
parentc3102fc92fc0c062bafdf20db7f0474393af2747 (diff)
downloadaoc-88c1d71d4148b8eb45e8b06c6f80d3014cc5a3da.tar.xz
aoc-88c1d71d4148b8eb45e8b06c6f80d3014cc5a3da.zip
Day 7 done
Diffstat (limited to '2023/Rust/src')
-rw-r--r--2023/Rust/src/day7.rs91
1 files changed, 55 insertions, 36 deletions
diff --git a/2023/Rust/src/day7.rs b/2023/Rust/src/day7.rs
index ce7b20f..048c8dc 100644
--- a/2023/Rust/src/day7.rs
+++ b/2023/Rust/src/day7.rs
@@ -45,6 +45,19 @@ const LABELS_JOKERS: [char; 13] = [
'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A',
];
+fn stronger_joker(a: &str, b: &str) -> Option<Ordering> {
+ for (first, second) in a.chars().zip(b.chars()) {
+ let pos_first = LABELS_JOKERS.iter().position(|&x| x == first);
+ let pos_second = LABELS_JOKERS.iter().position(|&x| x == second);
+ if pos_first > pos_second {
+ return Some(Ordering::Greater);
+ } else if pos_second > pos_first {
+ return Some(Ordering::Less);
+ }
+ }
+ Some(Ordering::Equal)
+}
+
fn stronger(a: &str, b: &str) -> Option<Ordering> {
for (first, second) in a.chars().zip(b.chars()) {
let pos_first = LABELS.iter().position(|&x| x == first);
@@ -94,28 +107,25 @@ impl<'a> PartialOrd for HandJokers<'a> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
use HandJokers::*;
match (self, other) {
- (FiveOfKind(a), FiveOfKind(b)) => stronger(a, b),
+ (FiveOfKind(a), FiveOfKind(b)) => stronger_joker(a, b),
(FiveOfKind(_), _) => Some(Ordering::Greater),
(_, FiveOfKind(_)) => Some(Ordering::Less),
- (FourOfKind(a), FourOfKind(b)) => stronger(a, b),
+ (FourOfKind(a), FourOfKind(b)) => stronger_joker(a, b),
(FourOfKind(_), _) => Some(Ordering::Greater),
(_, FourOfKind(_)) => Some(Ordering::Less),
- (FullHouse(a), FullHouse(b)) => stronger(a, b),
+ (FullHouse(a), FullHouse(b)) => stronger_joker(a, b),
(FullHouse(_), _) => Some(Ordering::Greater),
(_, FullHouse(_)) => Some(Ordering::Less),
- (ThreeOfKind(a), ThreeOfKind(b)) => stronger(a, b),
+ (ThreeOfKind(a), ThreeOfKind(b)) => stronger_joker(a, b),
(ThreeOfKind(_), _) => Some(Ordering::Greater),
(_, ThreeOfKind(_)) => Some(Ordering::Less),
- (TwoPair(a), TwoPair(b)) => stronger(a, b),
+ (TwoPair(a), TwoPair(b)) => stronger_joker(a, b),
(TwoPair(_), _) => Some(Ordering::Greater),
(_, TwoPair(_)) => Some(Ordering::Less),
- (OnePair(a), OnePair(b)) => stronger(a, b),
+ (OnePair(a), OnePair(b)) => stronger_joker(a, b),
(OnePair(_), _) => Some(Ordering::Greater),
(_, OnePair(_)) => Some(Ordering::Less),
- (HighCard(a), HighCard(b)) => stronger(a, b),
- (HighCard(_), _) => Some(Ordering::Greater),
- (_, HighCard(_)) => Some(Ordering::Less),
- (_, _) => None,
+ (HighCard(a), HighCard(b)) => stronger_joker(a, b),
}
}
}
@@ -143,9 +153,6 @@ impl<'a> PartialOrd for Hand<'a> {
(OnePair(_), _) => Some(Ordering::Greater),
(_, OnePair(_)) => Some(Ordering::Less),
(HighCard(a), HighCard(b)) => stronger(a, b),
- (HighCard(_), _) => Some(Ordering::Greater),
- (_, HighCard(_)) => Some(Ordering::Less),
- (_, _) => None,
}
}
}
@@ -175,6 +182,16 @@ impl<'a> From<&'a str> for HandJokers<'a> {
frq.entry(ch).and_modify(|f| *f += 1).or_insert(1);
}
+ let mut cur = 0usize;
+ let mut most_char = '1';
+
+ for (&ch, &f) in frq.iter() {
+ if ch != 'J' && f >= cur {
+ cur = f;
+ most_char = ch;
+ }
+ }
+
let jokers = frq.get(&'J');
let mut two_pairs = 0usize;
@@ -189,30 +206,32 @@ impl<'a> From<&'a str> for HandJokers<'a> {
}
}
- if let Some(&jokers) = jokers {
- match jokers {
- 2 => two_pairs -= 1,
- 3 => three_pairs -= 1,
- 4 => four_pairs -= 1,
- _ => {}
+ if let Some(_) = jokers {
+ use HandJokers::*;
+ let new_hand = hand.replace("J", &most_char.to_string());
+ let mut jfrq: HashMap<char, usize> = HashMap::new();
+ for ch in new_hand.chars() {
+ jfrq.entry(ch).and_modify(|f| *f += 1).or_insert(1);
+ }
+ let mut two_pairs = 0usize;
+ let mut three_pairs = 0usize;
+ let mut four_pairs = 0usize;
+ for (_, val) in jfrq.iter() {
+ match val {
+ 3 => three_pairs += 1,
+ 2 => two_pairs += 1,
+ 4 => four_pairs += 1,
+ _ => {}
+ }
}
- // println!("{hand:?}");
- match (frq.len(), two_pairs, three_pairs, four_pairs, jokers) {
- (1, _, _, _, _) => FiveOfKind(hand),
- (_, _, _, 1, 1) => FiveOfKind(hand),
- (_, 1, 0, _, 3) => FiveOfKind(hand),
- (_, _, 1, _, 2) => FiveOfKind(hand),
- (_, _, 1, _, 1) => FourOfKind(hand),
- (_, 0, _, _, 4) => FourOfKind(hand),
- (_, 1, 0, _, 2) => FourOfKind(hand),
- (_, 2, 0, _, 1) => FullHouse(hand),
- (_, 1, 0, _, 1) => ThreeOfKind(hand),
- (_, 0, 0, _, 3) => ThreeOfKind(hand),
- // A23AJ
- // J23AJ
- // 1234J
- (4, _, _, _, 2) => ThreeOfKind(hand),
- (5, _, _, _, 1) => OnePair(hand),
+ match (jfrq.len(), two_pairs, three_pairs, four_pairs) {
+ (5, _, _, _) => HighCard(hand),
+ (4, _, _, _) => OnePair(hand),
+ (1, _, _, _) => FiveOfKind(hand),
+ (_, _, _, 1) => FourOfKind(hand),
+ (_, 0, 1, _) => ThreeOfKind(hand),
+ (_, 1, 1, _) => FullHouse(hand),
+ (_, 2, 0, _) => TwoPair(hand),
_ => {
unreachable!()
}