day7 done
This commit is contained in:
@@ -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,12 +23,16 @@ enum HandType {
|
||||
ThreeOfAKind,
|
||||
FullHouse,
|
||||
FourOfAKind,
|
||||
FiveOfAKind
|
||||
FiveOfAKind,
|
||||
}
|
||||
|
||||
impl Hand{
|
||||
impl Hand<'_> {
|
||||
fn get_hand_type(&self) -> HandType {
|
||||
let uniq_cards: Vec<char> = self.cards.chars().into_iter().collect::<HashSet<_>>()
|
||||
let uniq_cards: Vec<char> = self
|
||||
.cards
|
||||
.chars()
|
||||
.into_iter()
|
||||
.collect::<HashSet<_>>()
|
||||
.into_iter()
|
||||
.collect::<Vec<char>>();
|
||||
|
||||
@@ -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<Hand> = 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::<Vec<char>>()[i];
|
||||
let othercard = other.cards.chars().collect::<Vec<char>>()[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 CARDS.find(selfcard) < CARDS.find(othercard){
|
||||
return Ordering::Less
|
||||
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::<Vec<char>>()[i];
|
||||
let othercard = other.original_cards.chars().collect::<Vec<char>>()[i];
|
||||
if self.cards_ranking.find(selfcard) > other.cards_ranking.find(othercard) {
|
||||
return Ordering::Greater;
|
||||
}
|
||||
if self.cards_ranking.find(selfcard) < other.cards_ranking.find(othercard) {
|
||||
return Ordering::Less;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,30 +98,26 @@ impl Ord for Hand {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Hand {
|
||||
impl PartialOrd for Hand<'_> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
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::<Vec<&str>>();
|
||||
let mut hands = c![Hand{
|
||||
cards: String::from(line.split(" ").collect::<Vec<&str>>()[0]),
|
||||
bid: line.split(" ").collect::<Vec<&str>>()[1].parse::<u32>().unwrap()
|
||||
bid: line.split(" ").collect::<Vec<&str>>()[1].parse::<u32>().unwrap(),
|
||||
cards_ranking: CARDS,
|
||||
original_cards: String::from(line.split(" ").collect::<Vec<&str>>()[0])
|
||||
}, for line in lines];
|
||||
let mut score: i64 = 0;
|
||||
hands.sort();
|
||||
@@ -115,12 +132,22 @@ fn part2(content: String) -> i64 {
|
||||
let lines = content.split("\n").collect::<Vec<&str>>();
|
||||
let mut hands = c![Hand{
|
||||
cards: String::from(line.split(" ").collect::<Vec<&str>>()[0]),
|
||||
bid: line.split(" ").collect::<Vec<&str>>()[1].parse::<u32>().unwrap()
|
||||
bid: line.split(" ").collect::<Vec<&str>>()[1].parse::<u32>().unwrap(),
|
||||
cards_ranking: CARDS2,
|
||||
original_cards: String::from(line.split(" ").collect::<Vec<&str>>()[0])
|
||||
}, for line in lines];
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
for h in hands{
|
||||
println!("{}:{}\twas\t{}", h.cards, h.bid, h.original_cards)
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user