-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd07.rs
More file actions
190 lines (162 loc) Β· 4.53 KB
/
d07.rs
File metadata and controls
190 lines (162 loc) Β· 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::collections::HashMap;
use crate::Day;
pub struct Day07 {}
impl Day for Day07 {
fn year(&self) -> u16 {
2023
}
fn day(&self) -> u8 {
7
}
fn part_one(&self) -> String {
let wildcard = false;
let mut hands = parse_input(&self.read_default_input(), wildcard);
sort_hands(&mut hands, wildcard);
total_winnings(&hands).to_string()
}
fn part_two(&self) -> String {
let wildcard = true;
let mut hands = parse_input(&self.read_default_input(), wildcard);
sort_hands(&mut hands, wildcard);
total_winnings(&hands).to_string()
}
}
type Hand = ([char; 5], HandType, u32);
fn parse_input(input: &str, wildcard: bool) -> Vec<Hand> {
input
.lines()
.map(|line| {
let tokens = line.split(' ').collect::<Vec<&str>>();
let hand: [char; 5] = tokens[0].chars().collect::<Vec<char>>().try_into().unwrap();
let strength = calculate_hand(&hand, wildcard);
(hand, strength, tokens[1].parse::<u32>().unwrap())
})
.collect::<Vec<Hand>>()
}
fn sort_hands(hands: &mut [Hand], wildcard: bool) {
hands.sort_by(|left, right| {
if left.1 != right.1 {
return left.1.partial_cmp(&right.1).unwrap();
}
for i in 0..5 {
let left_label = left.0[i];
let left_value = calculate_label(left_label, wildcard);
let right_label = right.0[i];
let right_value = calculate_label(right_label, wildcard);
let res = left_value.cmp(&right_value);
if res.is_eq() {
continue;
}
return res;
}
unreachable!()
});
}
fn total_winnings(hands: &[Hand]) -> u32 {
let mut result = 0;
for (i, (_, _, bid)) in hands.iter().enumerate() {
result += (i + 1) as u32 * bid;
}
result
}
fn calculate_label(label: char, wildcard: bool) -> u8 {
match label {
'A' => 14,
'K' => 13,
'Q' => 12,
'J' => {
if wildcard {
1
} else {
11
}
}
'T' => 10,
'9' => 9,
'8' => 8,
'7' => 7,
'6' => 6,
'5' => 5,
'4' => 4,
'3' => 3,
'2' => 2,
_ => unreachable!(),
}
}
fn calculate_hand(hand: &[char; 5], wildcard: bool) -> HandType {
let mut agg: HashMap<char, u32> = HashMap::new();
for card in hand {
let count = agg.entry(*card).or_insert(0);
*count += 1;
}
let default_strength = if agg.len() == 1 {
HandType::FiveOfAKind
} else if agg.len() == 2 {
let max_count = *agg.values().max().unwrap();
if max_count == 4 {
HandType::FourOfAKind
} else {
HandType::FullHouse
}
} else if agg.len() == 3 {
let max_count = *agg.values().max().unwrap();
if max_count == 3 {
HandType::ThreeOfAKind
} else {
HandType::TwoPair
}
} else if agg.len() == 4 {
HandType::OnePair
} else {
HandType::HighCard
};
if !wildcard || !hand.contains(&'J') {
return default_strength;
}
match default_strength {
HandType::FiveOfAKind => HandType::FiveOfAKind,
HandType::FourOfAKind => HandType::FiveOfAKind,
HandType::FullHouse => HandType::FiveOfAKind,
HandType::ThreeOfAKind => HandType::FourOfAKind,
HandType::TwoPair => {
let j_count = hand.iter().filter(|label| **label == 'J').count();
if j_count == 1 {
HandType::FullHouse
} else if j_count == 2 {
HandType::FourOfAKind
} else {
unreachable!()
}
}
HandType::OnePair => HandType::ThreeOfAKind,
HandType::HighCard => HandType::OnePair,
}
}
#[derive(PartialEq, Eq)]
enum HandType {
FiveOfAKind,
FourOfAKind,
FullHouse,
ThreeOfAKind,
TwoPair,
OnePair,
HighCard,
}
impl HandType {
fn value(&self) -> u8 {
match self {
HandType::FiveOfAKind => 7,
HandType::FourOfAKind => 6,
HandType::FullHouse => 5,
HandType::ThreeOfAKind => 4,
HandType::TwoPair => 3,
HandType::OnePair => 2,
HandType::HighCard => 1,
}
}
}
impl PartialOrd for HandType {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.value().cmp(&other.value()))
}
}