day 7 done
This commit is contained in:
@@ -126,17 +126,23 @@ impl Ord for Hand {
|
|||||||
.iter()
|
.iter()
|
||||||
.interleave(other.cards.iter())
|
.interleave(other.cards.iter())
|
||||||
.tuples::<(_, _)>()
|
.tuples::<(_, _)>()
|
||||||
.find_map(|(a, b)| {
|
.find_map(|(a, b)| match a.cmp(b) {
|
||||||
match a.cmp(b) {
|
Ordering::Equal => None,
|
||||||
Ordering::Equal => None,
|
x => Some(x),
|
||||||
x => Some(x)
|
})
|
||||||
}
|
.unwrap_or(Ordering::Equal),
|
||||||
}).unwrap_or(Ordering::Equal),
|
|
||||||
x => x,
|
x => x,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// part1 of day 7 of AOC 2023
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// - input the puszzle input
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
/// panics whenever the input isn't parsable
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn part1(input: &str) -> String {
|
pub fn part1(input: &str) -> String {
|
||||||
let (_, mut hands) = parse_input(input).expect("always valid input");
|
let (_, mut hands) = parse_input(input).expect("always valid input");
|
||||||
|
|||||||
@@ -6,11 +6,12 @@ use std::{
|
|||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Day1Part2Error;
|
struct Day1Part2Error;
|
||||||
|
|
||||||
#[derive(Debug, Ord, Eq, PartialEq, PartialOrd)]
|
#[derive(Debug, Ord, Eq, PartialEq, PartialOrd, Copy, Clone)]
|
||||||
enum Card {
|
enum Card {
|
||||||
Joker = 1,
|
Joker = 1,
|
||||||
Two,
|
Two,
|
||||||
@@ -67,6 +68,26 @@ impl From<&Card> for &u32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl fmt::Display for Card {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let c = match self {
|
||||||
|
Card::Joker => 'J',
|
||||||
|
Card::Two => '2',
|
||||||
|
Card::Three => '3',
|
||||||
|
Card::Four => '4',
|
||||||
|
Card::Five => '5',
|
||||||
|
Card::Six => '6',
|
||||||
|
Card::Seven => '7',
|
||||||
|
Card::Eight => '8',
|
||||||
|
Card::Nine => '9',
|
||||||
|
Card::Ten => 'T',
|
||||||
|
Card::Queen => 'Q',
|
||||||
|
Card::King => 'K',
|
||||||
|
Card::Ace => 'A',
|
||||||
|
};
|
||||||
|
write!(f, "{c}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
|
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
enum HandType {
|
enum HandType {
|
||||||
@@ -95,19 +116,20 @@ impl From<&Hand> for HandType {
|
|||||||
.sorted_by(|a, b| b.1.cmp(a.1))
|
.sorted_by(|a, b| b.1.cmp(a.1))
|
||||||
.collect::<Vec<_>>()[..]
|
.collect::<Vec<_>>()[..]
|
||||||
{
|
{
|
||||||
[(_, x),..] if jokers + x == 5 => Self::FiveOfAKind,
|
[(_, x), ..] if jokers + x == 5 => Self::FiveOfAKind,
|
||||||
|
[] if jokers == 5 => Self::FiveOfAKind,
|
||||||
[(_, x), ..] if jokers + x == 4 => Self::FourOfAKind,
|
[(_, x), ..] if jokers + x == 4 => Self::FourOfAKind,
|
||||||
[(_, 3), (_, 2)] => Self::FullHouse,
|
[(_, 3), (_, 2)] => Self::FullHouse,
|
||||||
[(_, 2), (_, 2)] if jokers == 1 => Self::FullHouse,
|
[(_, 2), (_, 2)] if jokers == 1 => Self::FullHouse,
|
||||||
[(_, x), ..] if jokers + x == 3 => Self::ThreeOfAKind,
|
[(_, x), ..] if jokers + x == 3 => Self::ThreeOfAKind,
|
||||||
[(_, 2), (_, 2), ..] => Self::TwoPair,
|
[(_, 2), (_, 2), ..] => Self::TwoPair,
|
||||||
[(_, x), ..] if jokers + x == 2 => Self::OnePair,
|
[(_, x), ..] if jokers + x == 2 => Self::OnePair,
|
||||||
_ => Self::HighCard,
|
_ => Self::HighCard,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
||||||
struct Hand {
|
struct Hand {
|
||||||
pub cards: [Card; 5],
|
pub cards: [Card; 5],
|
||||||
pub bet: u32,
|
pub bet: u32,
|
||||||
@@ -128,17 +150,23 @@ impl Ord for Hand {
|
|||||||
.iter()
|
.iter()
|
||||||
.interleave(other.cards.iter())
|
.interleave(other.cards.iter())
|
||||||
.tuples::<(_, _)>()
|
.tuples::<(_, _)>()
|
||||||
.find_map(|(a, b)| {
|
.find_map(|(a, b)| match a.cmp(b) {
|
||||||
match a.cmp(b) {
|
Ordering::Equal => None,
|
||||||
Ordering::Equal => None,
|
x => Some(x),
|
||||||
x => Some(x)
|
})
|
||||||
}
|
.unwrap_or(Ordering::Equal),
|
||||||
}).unwrap_or(Ordering::Equal),
|
|
||||||
x => x,
|
x => x,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// part2 of day 7 of AOC 2023
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// - input the puszzle input
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
/// panics whenever the input isn't parsable
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn part2(input: &str) -> String {
|
pub fn part2(input: &str) -> String {
|
||||||
let (_, mut hands) = parse_input(input).expect("always valid input");
|
let (_, mut hands) = parse_input(input).expect("always valid input");
|
||||||
|
|||||||
Reference in New Issue
Block a user