add 54 (WIP)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "euler54"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,166 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Card {
|
||||
value: Value,
|
||||
suit: Suit,
|
||||
}
|
||||
|
||||
impl Card {
|
||||
fn from_str(s: &str) -> Self {
|
||||
assert_eq!(s.len(), 2);
|
||||
let mut chars = s.chars();
|
||||
let value = Value::from_char(chars.next().unwrap());
|
||||
let suit = Suit::from_char(chars.next().unwrap());
|
||||
Self { value, suit }
|
||||
}
|
||||
}
|
||||
|
||||
enum Suit {
|
||||
Diamonds,
|
||||
Hearts,
|
||||
Spades,
|
||||
Clubs,
|
||||
}
|
||||
|
||||
impl Suit {
|
||||
fn from_char(c: char) -> Self {
|
||||
match c {
|
||||
'D' => Self::Diamonds,
|
||||
'H' => Self::Hearts,
|
||||
'S' => Self::Spades,
|
||||
'C' => Self::Clubs,
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Value {
|
||||
Two,
|
||||
Three,
|
||||
Four,
|
||||
Five,
|
||||
Six,
|
||||
Seven,
|
||||
Eight,
|
||||
Nine,
|
||||
Ten,
|
||||
Jack,
|
||||
Queen,
|
||||
King,
|
||||
Ace,
|
||||
}
|
||||
|
||||
impl Value {
|
||||
fn from_char(c: char) -> Self {
|
||||
match c {
|
||||
'2' => Self::Two,
|
||||
'3' => Self::Three,
|
||||
'4' => Self::Four,
|
||||
'5' => Self::Five,
|
||||
'6' => Self::Six,
|
||||
'7' => Self::Seven,
|
||||
'8' => Self::Eight,
|
||||
'9' => Self::Nine,
|
||||
'T' => Self::Ten,
|
||||
'J' => Self::Jack,
|
||||
'Q' => Self::Queen,
|
||||
'K' => Self::King,
|
||||
'A' => Self::Ace,
|
||||
_ => panic!("{c}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Hand {
|
||||
cards: [Card; 5],
|
||||
evaluation: Evaluation,
|
||||
}
|
||||
|
||||
struct Game {
|
||||
player1: Hand,
|
||||
player2: Hand,
|
||||
}
|
||||
|
||||
enum Evaluation {
|
||||
High,
|
||||
OnePair,
|
||||
TwoPair,
|
||||
ThreeOfAKind,
|
||||
Straight,
|
||||
Flush,
|
||||
FullHouse,
|
||||
FourOfAKind,
|
||||
StraightFlush,
|
||||
RoyalFlush,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
fn from_line(line: &str) -> Self {
|
||||
assert_eq!(line.len(), 29);
|
||||
let (p1, p2) = Self::divide_into_two_hands(line);
|
||||
let player1 = Hand::from_line(p1);
|
||||
let player2 = Hand::from_line(p2);
|
||||
Self { player1, player2 }
|
||||
}
|
||||
|
||||
fn divide_into_two_hands(line: &str) -> (&str, &str) {
|
||||
let (p1, p2) = line.split_at(15);
|
||||
(&p1[..14], p2)
|
||||
}
|
||||
}
|
||||
|
||||
impl Hand {
|
||||
fn from_line(line: &str) -> Self {
|
||||
assert_eq!(line.len(), 14);
|
||||
let vec: Vec<_> = line.split_whitespace().map(Card::from_str).collect();
|
||||
let cards = vec.try_into().unwrap_or_else(|_| panic!());
|
||||
let evaluation = Evaluation::from_cards(&cards);
|
||||
Self { cards, evaluation }
|
||||
}
|
||||
}
|
||||
|
||||
impl Evaluation {
|
||||
fn from_cards(cards: &[Card; 5]) -> Self {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn is_royal_flush(cards: &[Card; 5]) -> Option<Suit> {
|
||||
let values = [
|
||||
Value::Ten,
|
||||
Value::Jack,
|
||||
Value::Queen,
|
||||
Value::King,
|
||||
Value::Ace,
|
||||
];
|
||||
for value in values {
|
||||
if !cards.contains_val(value) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let mut suits = HashSet::new();
|
||||
for card in cards {
|
||||
suits.insert(card.suit)
|
||||
}
|
||||
if suits.len() == 1 {
|
||||
Some(suits.iter().next().unwrap())
|
||||
|
||||
|
||||
} else { None }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::Game;
|
||||
|
||||
#[test]
|
||||
fn example1() {
|
||||
let line = "5H 5C 6S 7S KD 2C 3S 8S 8D TD";
|
||||
let game = Game::from_line(line);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user