Solved part 2 of day 7
Now works with real puzzle input Problem was: Letters to strength count was initialized in the wrong way J was included twice.
This commit is contained in:
parent
480bc48d79
commit
e3f7039080
6 changed files with 212 additions and 27 deletions
|
@ -1,4 +1,5 @@
|
|||
use derive_more::derive::Into;
|
||||
mod joker_ordered;
|
||||
pub use joker_ordered::JokerOrdered;
|
||||
|
||||
use crate::solutions::day7::second_ordering;
|
||||
use std::usize;
|
||||
|
@ -11,30 +12,6 @@ pub struct CategorizedHand {
|
|||
rest: DealtHand,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Into)]
|
||||
pub struct JokerOrdered(pub CategorizedHand);
|
||||
|
||||
impl JokerOrdered {
|
||||
pub fn new(value: CategorizedHand) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for JokerOrdered {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
hand_cmp(
|
||||
&self.0,
|
||||
&other.0,
|
||||
&second_ordering::LABEL_ORDERING_WITH_JOKER,
|
||||
)
|
||||
}
|
||||
}
|
||||
impl PartialOrd for JokerOrdered {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl CategorizedHand {
|
||||
#[cfg(test)]
|
||||
pub fn new(kind: HandKind, rest: DealtHand) -> Self {
|
||||
|
|
73
src/solutions/day7/categorized_hand/joker_ordered.rs
Normal file
73
src/solutions/day7/categorized_hand/joker_ordered.rs
Normal file
|
@ -0,0 +1,73 @@
|
|||
use derive_more::derive::Into;
|
||||
|
||||
use crate::solutions::day7::second_ordering;
|
||||
|
||||
use super::{hand_cmp, CategorizedHand};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Into)]
|
||||
pub struct JokerOrdered(pub CategorizedHand);
|
||||
|
||||
impl JokerOrdered {
|
||||
pub fn new(value: CategorizedHand) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for JokerOrdered {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
hand_cmp(
|
||||
&self.0,
|
||||
&other.0,
|
||||
&second_ordering::LABEL_ORDERING_WITH_JOKER,
|
||||
)
|
||||
}
|
||||
}
|
||||
impl PartialOrd for JokerOrdered {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod testing {
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use crate::solutions::day7::{
|
||||
categorized_hand::{CategorizedHand, JokerOrdered},
|
||||
dealt_hand::DealtHand,
|
||||
second_ordering::{self, JOKER},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn order_with_jokers() {
|
||||
fn assert_case(left: &str, right: &str, expected: Ordering) {
|
||||
let (left, right): (CategorizedHand, CategorizedHand) = (
|
||||
(DealtHand::just_label(left), JOKER).into(),
|
||||
(DealtHand::just_label(right), JOKER).into(),
|
||||
);
|
||||
let (left, right) = (JokerOrdered::new(left), JokerOrdered::new(right));
|
||||
let actual = left.cmp(&right);
|
||||
assert_eq!(expected, actual, "Left: {:?}\nRight: {:?}", left, right);
|
||||
}
|
||||
|
||||
assert_case("AAAJJ", "JBBBB", Ordering::Greater);
|
||||
assert_case("22JAB", "22234", Ordering::Less);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_sort_with_joker_as_weakest() {
|
||||
let mut input: Vec<JokerOrdered> = vec![
|
||||
DealtHand::new("32T3K", 765),
|
||||
DealtHand::new("T55J5", 684),
|
||||
DealtHand::new("KK677", 28),
|
||||
DealtHand::new("KTJJT", 220),
|
||||
DealtHand::new("QQQJA", 483),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|to_map| JokerOrdered::new((to_map, second_ordering::JOKER).into()))
|
||||
.collect();
|
||||
input.sort();
|
||||
|
||||
insta::assert_debug_snapshot!(input);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
---
|
||||
source: src/solutions/day7/categorized_hand/joker_ordered.rs
|
||||
expression: input
|
||||
---
|
||||
[
|
||||
JokerOrdered(
|
||||
CategorizedHand {
|
||||
kind: OnePair,
|
||||
rest: DealtHand {
|
||||
hand: "32T3K",
|
||||
bid: 765,
|
||||
},
|
||||
},
|
||||
),
|
||||
JokerOrdered(
|
||||
CategorizedHand {
|
||||
kind: TwoPair,
|
||||
rest: DealtHand {
|
||||
hand: "KK677",
|
||||
bid: 28,
|
||||
},
|
||||
},
|
||||
),
|
||||
JokerOrdered(
|
||||
CategorizedHand {
|
||||
kind: Four,
|
||||
rest: DealtHand {
|
||||
hand: "T55J5",
|
||||
bid: 684,
|
||||
},
|
||||
},
|
||||
),
|
||||
JokerOrdered(
|
||||
CategorizedHand {
|
||||
kind: Four,
|
||||
rest: DealtHand {
|
||||
hand: "QQQJA",
|
||||
bid: 483,
|
||||
},
|
||||
},
|
||||
),
|
||||
JokerOrdered(
|
||||
CategorizedHand {
|
||||
kind: Four,
|
||||
rest: DealtHand {
|
||||
hand: "KTJJT",
|
||||
bid: 220,
|
||||
},
|
||||
},
|
||||
),
|
||||
]
|
|
@ -10,6 +10,11 @@ pub struct DealtHand {
|
|||
}
|
||||
|
||||
impl DealtHand {
|
||||
#[cfg(test)]
|
||||
pub fn just_label(hand: impl Into<String>) -> Self {
|
||||
let hand = hand.into();
|
||||
Self { hand, bid: 1 }
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn new(hand: impl Into<String>, score: usize) -> Self {
|
||||
let hand = hand.into();
|
||||
|
|
|
@ -2,8 +2,8 @@ use std::{cmp::Ordering, collections::HashMap, ops::RangeInclusive, sync::LazyLo
|
|||
|
||||
use log::warn;
|
||||
|
||||
const JOKER: char = 'J';
|
||||
static LETTERS_WITHOUT_JOKER: &[char] = &['T', 'J', 'Q', 'K', 'A'];
|
||||
pub const JOKER: char = 'J';
|
||||
static LETTERS_WITHOUT_JOKER: &[char] = &['T', 'Q', 'K', 'A'];
|
||||
static LETTERS: &[char] = &['T', 'J', 'Q', 'K', 'A'];
|
||||
const NUMBERS: RangeInclusive<u32> = 0..=9;
|
||||
|
||||
|
@ -64,3 +64,16 @@ pub fn compare_along_str(
|
|||
);
|
||||
left_len.cmp(&right_len)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod testing {
|
||||
#[test]
|
||||
fn check_label_with_joker_orders() {
|
||||
let mut ordered: Vec<(char, usize)> = super::LABEL_ORDERING_WITH_JOKER
|
||||
.clone()
|
||||
.into_iter()
|
||||
.collect();
|
||||
ordered.sort_by_key(|to_extract_from| to_extract_from.1);
|
||||
insta::assert_debug_snapshot!(ordered);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
---
|
||||
source: src/solutions/day7/second_ordering.rs
|
||||
expression: ordered
|
||||
---
|
||||
[
|
||||
(
|
||||
'J',
|
||||
0,
|
||||
),
|
||||
(
|
||||
'0',
|
||||
1,
|
||||
),
|
||||
(
|
||||
'1',
|
||||
2,
|
||||
),
|
||||
(
|
||||
'2',
|
||||
3,
|
||||
),
|
||||
(
|
||||
'3',
|
||||
4,
|
||||
),
|
||||
(
|
||||
'4',
|
||||
5,
|
||||
),
|
||||
(
|
||||
'5',
|
||||
6,
|
||||
),
|
||||
(
|
||||
'6',
|
||||
7,
|
||||
),
|
||||
(
|
||||
'7',
|
||||
8,
|
||||
),
|
||||
(
|
||||
'8',
|
||||
9,
|
||||
),
|
||||
(
|
||||
'9',
|
||||
10,
|
||||
),
|
||||
(
|
||||
'T',
|
||||
11,
|
||||
),
|
||||
(
|
||||
'Q',
|
||||
12,
|
||||
),
|
||||
(
|
||||
'K',
|
||||
13,
|
||||
),
|
||||
(
|
||||
'A',
|
||||
14,
|
||||
),
|
||||
]
|
Loading…
Add table
Reference in a new issue