2023 day7 part1
This commit is contained in:
126
2023/day7/src/main.rs
Normal file
126
2023/day7/src/main.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
static CARDS: &'static str = "23456789TJQKA";
|
||||
|
||||
#[macro_use(c)]
|
||||
extern crate cute;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Hand {
|
||||
cards: String,
|
||||
bid: u32
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum HandType {
|
||||
HighCard,
|
||||
OnePair,
|
||||
TwoPair,
|
||||
ThreeOfAKind,
|
||||
FullHouse,
|
||||
FourOfAKind,
|
||||
FiveOfAKind
|
||||
}
|
||||
|
||||
impl Hand{
|
||||
fn get_hand_type(&self) -> HandType{
|
||||
let uniq_cards: Vec<char> = self.cards.chars().into_iter().collect::<HashSet<_>>()
|
||||
.into_iter()
|
||||
.collect::<Vec<char>>();
|
||||
|
||||
let mut cards = c![(key,self.cards.chars().filter(|char| char == &key).collect::<Vec<char>>().len()), for key in uniq_cards];
|
||||
cards.sort_by(|a,b|a.1.cmp(&b.1));
|
||||
match cards.last().unwrap().1 {
|
||||
5 => HandType::FiveOfAKind,
|
||||
4 => HandType::FourOfAKind,
|
||||
3 if cards.len() == 2 => HandType::FullHouse,
|
||||
3 => HandType::ThreeOfAKind,
|
||||
2 if cards.len() == 3 => HandType::TwoPair,
|
||||
2 => HandType::OnePair,
|
||||
1 => HandType::HighCard,
|
||||
_ => panic!("Unknown card in {cards:?}")
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_jokers(&self){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Hand {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.cards == other.cards
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq 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 CARDS.find(selfcard) < CARDS.find(othercard){
|
||||
return Ordering::Less
|
||||
}
|
||||
}
|
||||
}
|
||||
Ordering::Equal
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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()
|
||||
}, 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);
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
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()
|
||||
}, for line in lines];
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user