This commit is contained in:
JonOfUs 2023-12-07 09:25:31 +01:00
parent 2c3e222620
commit a55e26c4f9
4 changed files with 1180 additions and 1 deletions

5
res/07/example Normal file
View file

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

1000
res/07/input Normal file

File diff suppressed because it is too large Load diff

174
src/days/d07.rs Normal file
View file

@ -0,0 +1,174 @@
use std::{cmp::Ordering, collections::HashMap, fs};
pub fn solve() {
let path = "res/07/input";
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let mut hands: Vec<(Vec<u32>, usize)> = contents
.lines()
.map(|line| {
let (h_raw, b_raw) = line.split_once(" ").unwrap();
let bid = b_raw.parse::<usize>().unwrap();
let hand: Vec<u32> = h_raw
.chars()
.map(|c| {
if c.is_digit(10) {
return c.to_digit(10).unwrap();
}
match c {
'A' => 14,
'K' => 13,
'Q' => 12,
'J' => 11,
'T' => 10,
_ => panic!("Invalid card parsed"),
}
})
.collect();
(hand, bid)
})
.collect();
hands.sort_by(|a, b| {
let a_type = get_type(&a.0);
let b_type = get_type(&b.0);
let mut ord = a_type.cmp(&b_type);
if ord == Ordering::Equal {
for i in 0..5 {
ord = a.0[i].cmp(&b.0[i]);
if ord != Ordering::Equal {
break;
}
}
}
ord
});
let result: usize = hands
.iter()
.enumerate()
.map(|(i, (_, bid))| bid * (i + 1))
.sum();
println!("Result 1: {}", result);
let mut hands: Vec<(Vec<u32>, usize)> = contents
.lines()
.map(|line| {
let (h_raw, b_raw) = line.split_once(" ").unwrap();
let bid = b_raw.parse::<usize>().unwrap();
let hand: Vec<u32> = h_raw
.chars()
.map(|c| {
if c.is_digit(10) {
return c.to_digit(10).unwrap();
}
match c {
'A' => 14,
'K' => 13,
'Q' => 12,
'J' => 1,
'T' => 10,
_ => panic!("Invalid card parsed"),
}
})
.collect();
(hand, bid)
})
.collect();
hands.sort_by(|a, b| {
let a_type = get_type_j(&a.0);
let b_type = get_type_j(&b.0);
let mut ord = a_type.cmp(&b_type);
if ord == Ordering::Equal {
for i in 0..5 {
ord = a.0[i].cmp(&b.0[i]);
if ord != Ordering::Equal {
break;
}
}
}
ord
});
let result: usize = hands
.iter()
.enumerate()
.map(|(i, (_, bid))| bid * (i + 1))
.sum();
println!("Result 2: {}", result);
}
fn get_type_j(cards: &Vec<u32>) -> u32 {
// full house
let mut map = HashMap::<u32, u32>::new();
for card in cards {
*map.entry(*card).or_insert(0) += 1;
}
let j = map.get(&1).unwrap_or(&0);
let mut maps: Vec<(u32, u32)> = map
.iter()
.map(|(k, v)| (*k, *v))
.filter(|(k, _)| *k != 1)
.collect();
maps.sort_by(|a, b| b.1.cmp(&a.1));
if maps.len() == 0 {
return 6; // 5 jokers
}
if maps[0].1 + j == 5 {
6
} else if maps[0].1 + j == 4 {
5
} else if maps[0].1 + j == 3 && maps[1].1 == 2 || maps[0].1 == 3 && maps[1].1 + j == 2 {
4
} else if maps[0].1 + j == 3 {
3
} else if maps[0].1 == 2 && maps[1].1 + j == 2 {
2
} else if maps[0].1 + j == 2 {
1
} else {
0
}
}
fn get_type(cards: &Vec<u32>) -> u32 {
// full house
let mut map = HashMap::<u32, u32>::new();
for card in cards {
*map.entry(*card).or_insert(0) += 1;
}
let mut maps: Vec<(u32, u32)> = map.iter().map(|(k, v)| (*k, *v)).collect();
maps.sort_by(|a, b| b.1.cmp(&a.1));
if maps[0].1 == 5 {
6
} else if maps[0].1 == 4 {
5
} else if maps[0].1 == 3 && maps[1].1 == 2 {
4
} else if maps[0].1 == 3 {
3
} else if maps[0].1 == 2 && maps[1].1 == 2 {
2
} else if maps[0].1 == 2 {
1
} else {
0
}
}

View file

@ -1,7 +1,7 @@
pub mod days; pub mod days;
fn main() { fn main() {
days::d06::solve() days::d07::solve()
//_all_days() //_all_days()
} }