generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.rs
280 lines (238 loc) · 7.87 KB
/
07.rs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::collections::HashSet;
use itertools::Itertools;
advent_of_code::solution!(7);
const CARD_ORDERING: &str = "AKQJT98765432";
const CARD_ORDERING2: &str = "AKQT98765432J";
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum Part {
Part1,
Part2,
}
#[derive(Debug)]
struct Hand {
cards: Vec<char>,
pub bid: u32,
ordering: Part,
}
impl Ord for Hand {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let classification_self = if self.ordering == Part::Part1 {
HandClassification::classify_hand(&self.cards)
} else {
HandClassification::classify_hand_with_jokers(&self.cards)
};
let other_classification = if self.ordering == Part::Part1 {
HandClassification::classify_hand(&other.cards)
} else {
HandClassification::classify_hand_with_jokers(&other.cards)
};
if classification_self == other_classification {
// Do linear card search
let numbered_cards = self
.cards
.iter()
.zip(other.cards.iter())
.map(|(card_self, card_other)| {
(
(if self.ordering == Part::Part1 {
CARD_ORDERING
} else {
CARD_ORDERING2
})
.chars()
.position(|order_card| order_card == *card_self)
.expect("Always in CARD_ORDERING"),
(if self.ordering == Part::Part1 {
CARD_ORDERING
} else {
CARD_ORDERING2
})
.chars()
.position(|order_card| order_card == *card_other)
.expect("Always in CARD_ORDERING"),
)
})
.collect_vec();
for (self_card, other_card) in numbered_cards {
match self_card.cmp(&other_card) {
std::cmp::Ordering::Less | std::cmp::Ordering::Greater => {
return self_card.cmp(&other_card)
}
std::cmp::Ordering::Equal => {}
}
}
// Must be equal
return std::cmp::Ordering::Equal;
}
classification_self.cmp(&other_classification)
}
}
impl PartialEq for Hand {
fn eq(&self, other: &Self) -> bool {
self.cmp(other).is_eq()
}
}
impl PartialOrd for Hand {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Eq for Hand {}
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
enum HandClassification {
FiveOfAKind,
FourOfAKind,
FullHouse,
ThreeOfAKind,
TwoPair,
OnePair,
HighCard,
}
impl HandClassification {
pub fn classify_hand(cards: &[char]) -> Self {
let unique_cards: HashSet<char> = HashSet::from_iter(cards.iter().cloned());
if unique_cards.len() == 1 {
return Self::FiveOfAKind;
}
if unique_cards.len() == 2 {
let mut number_of_each_card = unique_cards
.iter()
.map(|card_value| cards.iter().filter(|card| **card == *card_value).count())
.collect_vec();
number_of_each_card.sort();
if number_of_each_card == vec![1, 4] {
return Self::FourOfAKind;
}
if number_of_each_card == vec![2, 3] {
return Self::FullHouse;
}
}
if unique_cards.len() == 3 {
let mut number_of_each_card = unique_cards
.iter()
.map(|card_value| cards.iter().filter(|card| **card == *card_value).count())
.collect_vec();
number_of_each_card.sort();
if number_of_each_card == vec![1, 1, 3] {
return Self::ThreeOfAKind;
}
if number_of_each_card == vec![1, 2, 2] {
return Self::TwoPair;
}
}
if unique_cards.len() == 4 {
let mut number_of_each_card = unique_cards
.iter()
.map(|card_value| cards.iter().filter(|card| **card == *card_value).count())
.collect_vec();
number_of_each_card.sort();
if number_of_each_card == vec![1, 1, 1, 2] {
return Self::OnePair;
}
}
Self::HighCard
}
pub fn classify_hand_with_jokers(cards: &[char]) -> Self {
let number_of_jokers = cards.iter().filter(|card| **card == 'J').count();
let no_jokers_cards = cards
.iter()
.filter(|card| **card != 'J')
.cloned()
.collect_vec();
CARD_ORDERING
.chars()
.combinations_with_replacement(number_of_jokers)
.map(|mut combo| {
combo.extend(no_jokers_cards.iter());
HandClassification::classify_hand(&combo)
})
.min()
.expect("Always at least one ordering")
}
}
fn hand_vec_from_str(s: &str, part: Part) -> Vec<Hand> {
s.lines()
.map(|line| {
if let Some((cards, bid)) = line.split_ascii_whitespace().collect_tuple() {
Hand {
cards: cards.chars().collect_vec(),
bid: bid.parse().expect("bid always an integer"),
ordering: part,
}
} else {
panic!("Invalid number of fields");
}
})
.collect_vec()
}
pub fn part_one(input: &str) -> Option<u32> {
let mut hands = hand_vec_from_str(input, Part::Part1);
hands.sort();
Some(
hands
.iter()
.rev()
.enumerate()
.fold(0, |acc, (rank, hand)| acc + (rank + 1) as u32 * hand.bid),
)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut hands = hand_vec_from_str(input, Part::Part2);
hands.sort();
Some(
hands
.iter()
.rev()
.enumerate()
.fold(0, |acc, (rank, hand)| acc + (rank + 1) as u32 * hand.bid),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples/part1", DAY));
assert_eq!(result, Some(6440));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples/part2", DAY));
assert_eq!(result, Some(5905));
}
#[test]
fn test_classify_hand() {
assert_eq!(
HandClassification::classify_hand(&"AAAAA".chars().collect_vec()),
HandClassification::FiveOfAKind
);
assert_eq!(
HandClassification::classify_hand(&"AA8AA".chars().collect_vec()),
HandClassification::FourOfAKind
);
assert_eq!(
HandClassification::classify_hand(&"23332".chars().collect_vec()),
HandClassification::FullHouse
);
assert_eq!(
HandClassification::classify_hand(&"TTT98".chars().collect_vec()),
HandClassification::ThreeOfAKind
);
assert_eq!(
HandClassification::classify_hand(&"23432".chars().collect_vec()),
HandClassification::TwoPair
);
assert_eq!(
HandClassification::classify_hand(&"A23A4".chars().collect_vec()),
HandClassification::OnePair
);
assert_eq!(
HandClassification::classify_hand(&"23456".chars().collect_vec()),
HandClassification::HighCard
);
assert_eq!(
HandClassification::classify_hand(&"32T3K".chars().collect_vec()),
HandClassification::OnePair
);
}
}