add more code for 54 (WIP)

This commit is contained in:
Dr. Matthias Ratajczak
2022-08-17 20:21:01 +02:00
parent 9285c726a7
commit 66d884e298
2 changed files with 39 additions and 13 deletions
+1 -1
View File
@@ -9,5 +9,5 @@ fn main() {
ways[i] += ways[i - value]; ways[i] += ways[i - value];
} }
} }
println!("{:#?}", ways); println!("{}", ways[TARGET]);
} }
+38 -12
View File
@@ -6,6 +6,7 @@ fn main() {
println!("Hello, world!"); println!("Hello, world!");
} }
#[derive(Debug, PartialEq)]
struct Card { struct Card {
value: Value, value: Value,
suit: Suit, suit: Suit,
@@ -21,6 +22,7 @@ impl Card {
} }
} }
#[derive(Debug, PartialEq, Hash, Eq, Clone)]
enum Suit { enum Suit {
Diamonds, Diamonds,
Hearts, Hearts,
@@ -40,6 +42,7 @@ impl Suit {
} }
} }
#[derive(Debug, PartialEq)]
enum Value { enum Value {
Two, Two,
Three, Three,
@@ -119,18 +122,33 @@ impl Hand {
fn from_line(line: &str) -> Self { fn from_line(line: &str) -> Self {
assert_eq!(line.len(), 14); assert_eq!(line.len(), 14);
let vec: Vec<_> = line.split_whitespace().map(Card::from_str).collect(); let vec: Vec<_> = line.split_whitespace().map(Card::from_str).collect();
let cards = vec.try_into().unwrap_or_else(|_| panic!()); let cards: [Card; 5] = vec.try_into().unwrap_or_else(|_| panic!());
let evaluation = Evaluation::from_cards(&cards); let evaluation = Evaluation::from_cards(&cards);
Self { cards, evaluation } Self { cards, evaluation }
} }
} }
trait Contains<T> {
fn contains_value(&self, x: T) -> bool;
}
impl Contains<Value> for &[Card] {
fn contains_value(&self, x: Value) -> bool {
for card in self.iter() {
if card.value == x {
return true;
}
}
false
}
}
impl Evaluation { impl Evaluation {
fn from_cards(cards: &[Card; 5]) -> Self { fn from_cards(cards: &[Card]) -> Self {
todo!() todo!()
} }
fn is_royal_flush(cards: &[Card; 5]) -> Option<Suit> { fn is_royal_flush(cards: &[Card]) -> Option<Suit> {
let values = [ let values = [
Value::Ten, Value::Ten,
Value::Jack, Value::Jack,
@@ -139,28 +157,36 @@ impl Evaluation {
Value::Ace, Value::Ace,
]; ];
for value in values { for value in values {
if !cards.contains_val(value) { if !cards.contains_value(value) {
return None; return None;
} }
} }
let mut suits = HashSet::new(); if Self::have_matching_suits(cards) {
for card in cards { Some(cards[0].suit.clone())
suits.insert(card.suit) } else {
None
} }
if suits.len() == 1 { }
Some(suits.iter().next().unwrap())
fn have_matching_suits(cards: &[Card]) -> bool {
} else { None } let first_suit = &cards[0].suit;
for card in cards {
if &card.suit != first_suit {
return false;
}
}
true
}
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::Game; use crate::{Card, Game};
#[test] #[test]
fn example1() { fn example1() {
let line = "5H 5C 6S 7S KD 2C 3S 8S 8D TD"; let line = "5H 5C 6S 7S KD 2C 3S 8S 8D TD";
let game = Game::from_line(line); let game = Game::from_line(line);
assert_eq!(game.player1.cards[0], Card::from_str("5H"));
} }
} }