summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2023-12-07 08:42:00 +0100
committernekineki <nekineki@nekineki.net>2023-12-07 08:42:00 +0100
commit4005c51e0dd2f4769df738a14177a4df0c6a5dfa (patch)
tree0d7d87629c4688c6baf888a9b58f9afd90d2efc7 /2023
parent6fd02904e834a819b67047df226e796ca1934330 (diff)
day07 part2
Diffstat (limited to '2023')
-rw-r--r--2023/day07.rs55
1 files changed, 39 insertions, 16 deletions
diff --git a/2023/day07.rs b/2023/day07.rs
index eeb0763..143f7fe 100644
--- a/2023/day07.rs
+++ b/2023/day07.rs
@@ -1,6 +1,4 @@
-#![allow(dead_code)]
-#![allow(unused_variables)]
-#![allow(unused_mut)]
+use std::cmp::max;
use std::cmp::Ordering;
use std::fs::File;
use std::io::Read;
@@ -8,6 +6,7 @@ use std::io::Read;
#[derive(Debug)]
struct Hand {
cards: Vec<u8>,
+ ct: u32,
bid: u32,
}
@@ -45,19 +44,38 @@ fn card_type(count: &Vec<u32>) -> u32 {
}
}
-fn compare_cards(c1: &Vec<u8>, c2: &Vec<u8>) -> Ordering {
- let count1 = count_cards(c1);
- let count2 = count_cards(c2);
- let type1 = card_type(&count1);
- let type2 = card_type(&count2);
+fn max_type(count: &Vec<u32>, joker: u32) -> u32 {
+ if joker == 0 {
+ return card_type(count);
+ } else {
+ let mut mt = 0;
+ for i in 0..count.len() {
+ let mut c = count.to_vec();
+ c[i] += 1;
+ mt = max(mt, max_type(&c, joker - 1));
+ }
+ return mt;
+ }
+}
+
+fn get_card_type(c: &Vec<u8>) -> u32 {
+ let c_noj: Vec<u8> = c.iter().filter(|x| **x != 0).copied().collect();
+ let joker = c
+ .iter()
+ .fold(0, |acc, x| if *x == 0 { acc + 1 } else { acc });
+ let mut count = count_cards(&c_noj);
+ count.push(0);
+ max_type(&count, joker)
+}
+
+fn compare_cards(c1: &Vec<u8>, type1: u32, c2: &Vec<u8>, type2: u32) -> Ordering {
if type1 > type2 {
return Ordering::Greater;
} else if type1 < type2 {
return Ordering::Less;
} else {
for (e1, e2) in c1.iter().zip(c2.iter()) {
- // println!("{} {} {}", e1, e2, e1 > d2);
if e1 > e2 {
return Ordering::Greater;
} else if e1 < e2 {
@@ -69,8 +87,11 @@ fn compare_cards(c1: &Vec<u8>, c2: &Vec<u8>) -> Ordering {
}
fn string_to_card(s: &str) -> Vec<u8> {
+ // let map = [
+ // '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A',
+ // ];
let map = [
- '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A',
+ 'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A',
];
let mut out = Vec::new();
for c in s.chars() {
@@ -87,25 +108,27 @@ fn main() {
let mut content = String::new();
f.read_to_string(&mut content).expect("cannot read file");
let lines: Vec<_> = content.trim_end().split('\n').collect();
- // println!("{:?}", lines);
let mut hands: Vec<Hand> = Vec::new();
for line in lines.iter() {
let (cards, bid) = line.split_once(" ").unwrap();
+ let cards = string_to_card(cards);
hands.push(Hand {
- cards: string_to_card(cards),
+ cards: cards.clone(),
bid: bid.parse().unwrap(),
+ ct: get_card_type(&cards),
});
}
- hands.sort_unstable_by(|x, y| compare_cards(&x.cards, &y.cards));
+ hands.sort_by(|x, y| compare_cards(&x.cards, x.ct, &y.cards, y.ct));
let mut res1 = 0;
- for (i, h) in hands.iter().enumerate() {
- res1 += (i as u32 + 1) * h.bid;
- }
let mut res2 = 0;
+ for (i, h) in hands.iter().enumerate() {
+ // println!("{:?} {}", h.cards, h.ct);
+ res2 += (i as u32 + 1) * h.bid;
+ }
println!("res1: {}", res1);
println!("res2: {}", res2);