From e3f7039080d2659057d057b7bf6f118692990029 Mon Sep 17 00:00:00 2001 From: BoolPurist Date: Fri, 23 Aug 2024 12:08:39 +0200 Subject: [PATCH] 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. --- src/solutions/day7/categorized_hand.rs | 27 +------ .../day7/categorized_hand/joker_ordered.rs | 73 +++++++++++++++++++ ...g__example_sort_with_joker_as_weakest.snap | 51 +++++++++++++ src/solutions/day7/dealt_hand.rs | 5 ++ src/solutions/day7/second_ordering.rs | 17 ++++- ...esting__check_label_with_joker_orders.snap | 66 +++++++++++++++++ 6 files changed, 212 insertions(+), 27 deletions(-) create mode 100644 src/solutions/day7/categorized_hand/joker_ordered.rs create mode 100644 src/solutions/day7/categorized_hand/snapshots/advent_of_code_2023__solutions__day7__categorized_hand__joker_ordered__testing__example_sort_with_joker_as_weakest.snap create mode 100644 src/solutions/day7/snapshots/advent_of_code_2023__solutions__day7__second_ordering__testing__check_label_with_joker_orders.snap diff --git a/src/solutions/day7/categorized_hand.rs b/src/solutions/day7/categorized_hand.rs index f68dfe8..74ddcb4 100644 --- a/src/solutions/day7/categorized_hand.rs +++ b/src/solutions/day7/categorized_hand.rs @@ -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 { - Some(self.cmp(other)) - } -} - impl CategorizedHand { #[cfg(test)] pub fn new(kind: HandKind, rest: DealtHand) -> Self { diff --git a/src/solutions/day7/categorized_hand/joker_ordered.rs b/src/solutions/day7/categorized_hand/joker_ordered.rs new file mode 100644 index 0000000..bda9704 --- /dev/null +++ b/src/solutions/day7/categorized_hand/joker_ordered.rs @@ -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 { + 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 = 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); + } +} diff --git a/src/solutions/day7/categorized_hand/snapshots/advent_of_code_2023__solutions__day7__categorized_hand__joker_ordered__testing__example_sort_with_joker_as_weakest.snap b/src/solutions/day7/categorized_hand/snapshots/advent_of_code_2023__solutions__day7__categorized_hand__joker_ordered__testing__example_sort_with_joker_as_weakest.snap new file mode 100644 index 0000000..35cfe60 --- /dev/null +++ b/src/solutions/day7/categorized_hand/snapshots/advent_of_code_2023__solutions__day7__categorized_hand__joker_ordered__testing__example_sort_with_joker_as_weakest.snap @@ -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, + }, + }, + ), +] diff --git a/src/solutions/day7/dealt_hand.rs b/src/solutions/day7/dealt_hand.rs index 9df5570..8ad9cf2 100644 --- a/src/solutions/day7/dealt_hand.rs +++ b/src/solutions/day7/dealt_hand.rs @@ -10,6 +10,11 @@ pub struct DealtHand { } impl DealtHand { + #[cfg(test)] + pub fn just_label(hand: impl Into) -> Self { + let hand = hand.into(); + Self { hand, bid: 1 } + } #[cfg(test)] pub fn new(hand: impl Into, score: usize) -> Self { let hand = hand.into(); diff --git a/src/solutions/day7/second_ordering.rs b/src/solutions/day7/second_ordering.rs index a4c6609..1d852b6 100644 --- a/src/solutions/day7/second_ordering.rs +++ b/src/solutions/day7/second_ordering.rs @@ -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 = 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); + } +} diff --git a/src/solutions/day7/snapshots/advent_of_code_2023__solutions__day7__second_ordering__testing__check_label_with_joker_orders.snap b/src/solutions/day7/snapshots/advent_of_code_2023__solutions__day7__second_ordering__testing__check_label_with_joker_orders.snap new file mode 100644 index 0000000..baa7001 --- /dev/null +++ b/src/solutions/day7/snapshots/advent_of_code_2023__solutions__day7__second_ordering__testing__check_label_with_joker_orders.snap @@ -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, + ), +]