summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2023-12-07 12:12:55 +0100
committernekineki <nekineki@nekineki.net>2023-12-07 12:13:41 +0100
commit4cebfb50135ce85f05e2ee64eb82c8a61debc092 (patch)
treebab3e6f2429e79acd8f02e923fd569eece369f04 /2023
parent149cec0f2a49c3124cb7422a9a201e2338d38525 (diff)
day07 solve both parts
Diffstat (limited to '2023')
-rw-r--r--2023/day07.rs85
1 files changed, 44 insertions, 41 deletions
diff --git a/2023/day07.rs b/2023/day07.rs
index 143f7fe..24635b3 100644
--- a/2023/day07.rs
+++ b/2023/day07.rs
@@ -1,4 +1,3 @@
-use std::cmp::max;
use std::cmp::Ordering;
use std::fs::File;
use std::io::Read;
@@ -17,8 +16,8 @@ fn count_cards(arr: &Vec<u8>) -> Vec<u32> {
}
let mut count = count
.iter()
- .filter(|x| **x != 0)
.copied()
+ .filter(|x| *x != 0)
.collect::<Vec<_>>();
count.sort();
count.reverse();
@@ -44,38 +43,29 @@ fn card_type(count: &Vec<u32>) -> u32 {
}
}
-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_type1(c: &Vec<u8>) -> u32 {
+ card_type(&count_cards(&c))
}
-fn get_card_type(c: &Vec<u8>) -> u32 {
- let c_noj: Vec<u8> = c.iter().filter(|x| **x != 0).copied().collect();
+fn get_card_type2(c: &Vec<u8>) -> u32 {
+ let c_noj: Vec<u8> = c.iter().copied().filter(|x| *x != 0).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);
+ count[0] += joker;
- max_type(&count, joker)
+ card_type(&count)
}
-fn compare_cards(c1: &Vec<u8>, type1: u32, c2: &Vec<u8>, type2: u32) -> Ordering {
- if type1 > type2 {
+fn compare_hands(h1: &Hand, h2: &Hand) -> Ordering {
+ if h1.ct > h2.ct {
return Ordering::Greater;
- } else if type1 < type2 {
+ } else if h1.ct < h2.ct {
return Ordering::Less;
} else {
- for (e1, e2) in c1.iter().zip(c2.iter()) {
+ for (e1, e2) in h1.cards.iter().zip(h2.cards.iter()) {
if e1 > e2 {
return Ordering::Greater;
} else if e1 < e2 {
@@ -86,18 +76,10 @@ fn compare_cards(c1: &Vec<u8>, type1: u32, c2: &Vec<u8>, type2: u32) -> Ordering
Ordering::Equal
}
-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 = [
- 'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A',
- ];
- let mut out = Vec::new();
- for c in s.chars() {
- out.push(map.iter().position(|x| x == &c).unwrap() as u8);
- }
- out
+fn string_to_card(s: &str, map: &[char]) -> Vec<u8> {
+ s.chars()
+ .map(|c| map.iter().position(|x| x == &c).unwrap() as u8)
+ .collect()
}
fn main() {
@@ -109,27 +91,48 @@ fn main() {
f.read_to_string(&mut content).expect("cannot read file");
let lines: Vec<_> = content.trim_end().split('\n').collect();
- let mut hands: Vec<Hand> = Vec::new();
+ let map1 = [
+ '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A',
+ ];
+
+ let map2 = [
+ 'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A',
+ ];
+
+ let mut hands1: Vec<Hand> = Vec::new();
+ let mut hands2: 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: cards.clone(),
+ let cards1 = string_to_card(&cards, &map1);
+ let cards2 = string_to_card(&cards, &map2);
+ hands1.push(Hand {
+ cards: cards1.clone(),
+ bid: bid.parse().unwrap(),
+ ct: get_card_type1(&cards1),
+ });
+ hands2.push(Hand {
+ cards: cards2.clone(),
bid: bid.parse().unwrap(),
- ct: get_card_type(&cards),
+ ct: get_card_type2(&cards2),
});
}
- hands.sort_by(|x, y| compare_cards(&x.cards, x.ct, &y.cards, y.ct));
-
+ hands1.sort_by(compare_hands);
let mut res1 = 0;
+ for (i, h) in hands1.iter().enumerate() {
+ // println!("{:?} {}", h.cards, h.ct);
+ res1 += (i as u32 + 1) * h.bid;
+ }
+ hands2.sort_by(compare_hands);
let mut res2 = 0;
- for (i, h) in hands.iter().enumerate() {
+ for (i, h) in hands2.iter().enumerate() {
// println!("{:?} {}", h.cards, h.ct);
res2 += (i as u32 + 1) * h.bid;
}
println!("res1: {}", res1);
println!("res2: {}", res2);
+ assert_eq!(res1, 250232501);
+ assert_eq!(res2, 249138943);
}