day7 done
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::cmp::Ordering;
|
|
||||||
|
|
||||||
static CARDS: &'static str = "23456789TJQKA";
|
static CARDS: &'static str = "23456789TJQKA";
|
||||||
|
static CARDS2: &'static str = "J23456789TQKA";
|
||||||
#[macro_use(c)]
|
#[macro_use(c)]
|
||||||
extern crate cute;
|
extern crate cute;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
struct Hand {
|
struct Hand<'a> {
|
||||||
cards: String,
|
cards: String,
|
||||||
bid: u32
|
bid: u32,
|
||||||
|
cards_ranking: &'a str,
|
||||||
|
original_cards: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -21,17 +23,21 @@ enum HandType {
|
|||||||
ThreeOfAKind,
|
ThreeOfAKind,
|
||||||
FullHouse,
|
FullHouse,
|
||||||
FourOfAKind,
|
FourOfAKind,
|
||||||
FiveOfAKind
|
FiveOfAKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hand{
|
impl Hand<'_> {
|
||||||
fn get_hand_type(&self) -> HandType{
|
fn get_hand_type(&self) -> HandType {
|
||||||
let uniq_cards: Vec<char> = self.cards.chars().into_iter().collect::<HashSet<_>>()
|
let uniq_cards: Vec<char> = self
|
||||||
.into_iter()
|
.cards
|
||||||
.collect::<Vec<char>>();
|
.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];
|
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));
|
cards.sort_by(|a, b| a.1.cmp(&b.1));
|
||||||
match cards.last().unwrap().1 {
|
match cards.last().unwrap().1 {
|
||||||
5 => HandType::FiveOfAKind,
|
5 => HandType::FiveOfAKind,
|
||||||
4 => HandType::FourOfAKind,
|
4 => HandType::FourOfAKind,
|
||||||
@@ -40,36 +46,51 @@ impl Hand{
|
|||||||
2 if cards.len() == 3 => HandType::TwoPair,
|
2 if cards.len() == 3 => HandType::TwoPair,
|
||||||
2 => HandType::OnePair,
|
2 => HandType::OnePair,
|
||||||
1 => HandType::HighCard,
|
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 {
|
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 {
|
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 {
|
||||||
if (self.get_hand_type() as u8) < other.get_hand_type() as u8 { return Ordering::Less; }
|
return Ordering::Greater;
|
||||||
else{
|
}
|
||||||
for i in 0..5{
|
if (self.get_hand_type() as u8) < other.get_hand_type() as u8 {
|
||||||
let selfcard = self.cards.chars().collect::<Vec<char>>()[i];
|
return Ordering::Less;
|
||||||
let othercard = other.cards.chars().collect::<Vec<char>>()[i];
|
} else {
|
||||||
if CARDS.find(selfcard) > CARDS.find(othercard){
|
for i in 0..5 {
|
||||||
return Ordering::Greater
|
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 CARDS.find(selfcard) < CARDS.find(othercard){
|
if self.cards_ranking.find(selfcard) < other.cards_ranking.find(othercard) {
|
||||||
return Ordering::Less
|
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<Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let content = fs::read_to_string("input").expect("Could not read the file");
|
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());
|
let part1 = part1(content.clone());
|
||||||
println!("Part 1: {}", part1);
|
println!("Part 1: {}", part1);
|
||||||
let part2 = part2(content);
|
let part2 = part2(content);
|
||||||
println!("Part 1: {}", part2);
|
println!("Part 2: {}", part2);
|
||||||
}
|
}
|
||||||
fn part1(content: String) -> i64 {
|
fn part1(content: String) -> i64 {
|
||||||
let lines = content.split("\n").collect::<Vec<&str>>();
|
let lines = content.split("\n").collect::<Vec<&str>>();
|
||||||
let mut hands = c![Hand{
|
let mut hands = c![Hand{
|
||||||
cards: String::from(line.split(" ").collect::<Vec<&str>>()[0]),
|
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(),
|
||||||
}, for line in lines];
|
cards_ranking: CARDS,
|
||||||
|
original_cards: String::from(line.split(" ").collect::<Vec<&str>>()[0])
|
||||||
|
}, for line in lines];
|
||||||
let mut score: i64 = 0;
|
let mut score: i64 = 0;
|
||||||
hands.sort();
|
hands.sort();
|
||||||
for i in 0..hands.len() {
|
for i in 0..hands.len() {
|
||||||
let hand = hands.get(i).unwrap();
|
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;
|
return score;
|
||||||
}
|
}
|
||||||
@@ -115,12 +132,22 @@ fn part2(content: String) -> i64 {
|
|||||||
let lines = content.split("\n").collect::<Vec<&str>>();
|
let lines = content.split("\n").collect::<Vec<&str>>();
|
||||||
let mut hands = c![Hand{
|
let mut hands = c![Hand{
|
||||||
cards: String::from(line.split(" ").collect::<Vec<&str>>()[0]),
|
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];
|
}, 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
for h in hands{
|
||||||
|
println!("{}:{}\twas\t{}", h.cards, h.bid, h.original_cards)
|
||||||
return 0;
|
}
|
||||||
|
return score;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user