From fc62a8da5856486791aca761a96a9ad587bceb00 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 13 Nov 2024 12:16:17 +0000 Subject: [PATCH] day7 done --- 2023/day7/src/main.rs | 119 ++++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 46 deletions(-) diff --git a/2023/day7/src/main.rs b/2023/day7/src/main.rs index a65b86d..66ffaeb 100644 --- a/2023/day7/src/main.rs +++ b/2023/day7/src/main.rs @@ -1,16 +1,18 @@ +use std::cmp::Ordering; use std::collections::HashSet; use std::fs; -use std::cmp::Ordering; static CARDS: &'static str = "23456789TJQKA"; - +static CARDS2: &'static str = "J23456789TQKA"; #[macro_use(c)] extern crate cute; -#[derive(Debug)] -struct Hand { +#[derive(Debug, Clone)] +struct Hand<'a> { cards: String, - bid: u32 + bid: u32, + cards_ranking: &'a str, + original_cards: String, } #[derive(Debug)] @@ -21,17 +23,21 @@ enum HandType { ThreeOfAKind, FullHouse, FourOfAKind, - FiveOfAKind + FiveOfAKind, } -impl Hand{ - fn get_hand_type(&self) -> HandType{ - let uniq_cards: Vec = self.cards.chars().into_iter().collect::>() - .into_iter() - .collect::>(); +impl Hand<'_> { + fn get_hand_type(&self) -> HandType { + let uniq_cards: Vec = self + .cards + .chars() + .into_iter() + .collect::>() + .into_iter() + .collect::>(); let mut cards = c![(key,self.cards.chars().filter(|char| char == &key).collect::>().len()), for key in uniq_cards]; - cards.sort_by(|a,b|a.1.cmp(&b.1)); + cards.sort_by(|a, b| a.1.cmp(&b.1)); match cards.last().unwrap().1 { 5 => HandType::FiveOfAKind, 4 => HandType::FourOfAKind, @@ -40,36 +46,51 @@ impl Hand{ 2 if cards.len() == 3 => HandType::TwoPair, 2 => HandType::OnePair, 1 => HandType::HighCard, - _ => panic!("Unknown card in {cards:?}") + _ => panic!("Unknown card in {cards:?}"), } } - fn replace_jokers(&self){ - + fn replace_jokers(&mut self) { + if self.cards.contains("J") { + let mut tmp_vec: Vec = Vec::new(); + for elem in CARDS.chars().filter(|letter| letter != &'J') { + tmp_vec.push(Hand { + cards: self.cards.clone().replace("J", elem.to_string().as_str()), + bid: 0, + cards_ranking: self.cards_ranking.clone(), + original_cards: self.original_cards.clone() + }); + } + tmp_vec.sort(); + self.cards = tmp_vec.last().unwrap().cards.clone(); + } } } -impl PartialEq for Hand { +impl PartialEq for Hand<'_> { fn eq(&self, other: &Self) -> bool { - self.cards == other.cards + self.original_cards == other.original_cards } } -impl Eq for Hand{} +impl Eq for Hand<'_> {} -impl Ord for Hand { +impl Ord for Hand<'_> { fn cmp(&self, other: &Self) -> Ordering { - if self.get_hand_type() as u8 > other.get_hand_type() as u8 { return Ordering::Greater; } - if (self.get_hand_type() as u8) < other.get_hand_type() as u8 { return Ordering::Less; } - else{ - for i in 0..5{ - let selfcard = self.cards.chars().collect::>()[i]; - let othercard = other.cards.chars().collect::>()[i]; - if CARDS.find(selfcard) > CARDS.find(othercard){ - return Ordering::Greater + if self.get_hand_type() as u8 > other.get_hand_type() as u8 { + return Ordering::Greater; + } + if (self.get_hand_type() as u8) < other.get_hand_type() as u8 { + return Ordering::Less; + } else { + for i in 0..5 { + let selfcard = self.original_cards.chars().collect::>()[i]; + let othercard = other.original_cards.chars().collect::>()[i]; + if self.cards_ranking.find(selfcard) > other.cards_ranking.find(othercard) { + return Ordering::Greater; } - if CARDS.find(selfcard) < CARDS.find(othercard){ - return Ordering::Less + if self.cards_ranking.find(selfcard) < other.cards_ranking.find(othercard) { + return Ordering::Less; } } } @@ -77,36 +98,32 @@ impl Ord for Hand { } } -impl PartialOrd for Hand { +impl PartialOrd for Hand<'_> { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } - fn main() { let content = fs::read_to_string("input").expect("Could not read the file"); -// let content = String::from("32T3K 765 -// T55J5 684 -// KK677 28 -// KTJJT 220 -// QQQJA 483"); let part1 = part1(content.clone()); println!("Part 1: {}", part1); let part2 = part2(content); - println!("Part 1: {}", part2); + println!("Part 2: {}", part2); } fn part1(content: String) -> i64 { let lines = content.split("\n").collect::>(); let mut hands = c![Hand{ cards: String::from(line.split(" ").collect::>()[0]), - bid: line.split(" ").collect::>()[1].parse::().unwrap() - }, for line in lines]; + bid: line.split(" ").collect::>()[1].parse::().unwrap(), + cards_ranking: CARDS, + original_cards: String::from(line.split(" ").collect::>()[0]) + }, for line in lines]; let mut score: i64 = 0; hands.sort(); for i in 0..hands.len() { let hand = hands.get(i).unwrap(); - score += hand.bid as i64 *(i as i64 + 1); + score += hand.bid as i64 * (i as i64 + 1); } return score; } @@ -115,12 +132,22 @@ fn part2(content: String) -> i64 { let lines = content.split("\n").collect::>(); let mut hands = c![Hand{ cards: String::from(line.split(" ").collect::>()[0]), - bid: line.split(" ").collect::>()[1].parse::().unwrap() + bid: line.split(" ").collect::>()[1].parse::().unwrap(), + cards_ranking: CARDS2, + original_cards: String::from(line.split(" ").collect::>()[0]) }, for line in lines]; + for h in &mut hands { + h.replace_jokers(); + } + let mut score: i64 = 0; + hands.sort(); + for i in 0..hands.len() { + let hand = hands.get(i).unwrap(); + score += hand.bid as i64 * (i as i64 + 1); + } - - - return 0; - - + for h in hands{ + println!("{}:{}\twas\t{}", h.cards, h.bid, h.original_cards) + } + return score; }