-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.rs
305 lines (282 loc) · 9.55 KB
/
day7.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use aoc_runner_derive::aoc;
#[aoc(day7, part1)]
#[must_use]
pub fn part1(input: &str) -> u64 {
let input: Vec<utils::CardHandWithBid<utils::part1::CamelCard>> = utils::parse(input);
utils::solve(&input)
}
#[aoc(day7, part2)]
#[must_use]
pub fn part2(input: &str) -> u64 {
let input: Vec<utils::CardHandWithBid<utils::part2::CamelCard>> = utils::parse(input);
utils::solve(&input)
}
mod utils {
use itertools::Itertools;
use strum::{EnumIter, IntoEnumIterator};
pub fn parse<T: Ord + Copy + TryFrom<char>>(input: &str) -> Vec<CardHandWithBid<T>>
where
HandType: From<[T; 5]>,
{
input
.lines()
.map(|line| {
let (cards, bid) = line.split_ascii_whitespace().collect_tuple().unwrap();
let cards: [T; 5] = cards
.chars()
.map(|card| {
card.try_into()
.unwrap_or_else(|_| panic!("Invalid card: {card}"))
})
.collect::<Vec<_>>()
.try_into()
.unwrap_or_else(|_| panic!("Invalid hand: {cards}"));
let hand_type = cards.into();
CardHandWithBid {
hand: CardHand { hand_type, cards },
bid: bid.parse().unwrap(),
}
})
.collect()
}
pub fn solve<T: Ord + Copy>(input: &[CardHandWithBid<T>]) -> u64 {
input
.iter()
.sorted_by_key(|card_hand| card_hand.hand)
.enumerate()
.map(|(i, card_hand)| u64::try_from(i + 1).unwrap() * card_hand.bid)
.sum()
}
pub struct CardHandWithBid<T: Ord> {
pub hand: CardHand<T>,
pub bid: u64,
}
#[derive(Clone, Copy, Eq)]
pub struct CardHand<T: Ord> {
pub hand_type: HandType,
pub cards: [T; 5],
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum HandType {
HighCard,
OnePair,
TwoPair,
ThreeOfKind,
FullHouse,
FourOfKind,
FiveOfKind,
}
impl<T: Ord> PartialEq for CardHand<T> {
fn eq(&self, other: &Self) -> bool {
self.cards.eq(&other.cards)
}
}
impl<T: Ord> PartialOrd for CardHand<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<T: Ord> Ord for CardHand<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.hand_type.cmp(&other.hand_type) {
std::cmp::Ordering::Equal => {
for i in 0..5 {
match self.cards[i].cmp(&other.cards[i]) {
std::cmp::Ordering::Equal => {}
other => return other,
}
}
std::cmp::Ordering::Equal
}
other => other,
}
}
}
pub mod part1 {
use super::{EnumIter, HandType, IntoEnumIterator};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, EnumIter)]
pub enum CamelCard {
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
T,
J,
Q,
K,
A,
}
impl TryFrom<char> for CamelCard {
type Error = &'static str;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'2' => Ok(Self::Two),
'3' => Ok(Self::Three),
'4' => Ok(Self::Four),
'5' => Ok(Self::Five),
'6' => Ok(Self::Six),
'7' => Ok(Self::Seven),
'8' => Ok(Self::Eight),
'9' => Ok(Self::Nine),
'T' => Ok(Self::T),
'J' => Ok(Self::J),
'Q' => Ok(Self::Q),
'K' => Ok(Self::K),
'A' => Ok(Self::A),
_ => Err("Invalid card"),
}
}
}
impl From<[CamelCard; 5]> for HandType {
fn from(value: [CamelCard; 5]) -> Self {
let mut hand_types_raw = smallvec::SmallVec::<[Self; 2]>::new();
for card in CamelCard::iter() {
match value.iter().filter(|&&c| c == card).count() {
2 => hand_types_raw.push(Self::OnePair),
3 => hand_types_raw.push(Self::ThreeOfKind),
4 => {
hand_types_raw.push(Self::FourOfKind);
break;
}
5 => {
hand_types_raw.push(Self::FiveOfKind);
break;
}
_ => {}
}
}
match hand_types_raw.len() {
0 => Self::HighCard,
1 => hand_types_raw[0],
2 => {
hand_types_raw.sort();
match hand_types_raw[0] {
Self::OnePair => match hand_types_raw[1] {
Self::OnePair => Self::TwoPair,
Self::ThreeOfKind => Self::FullHouse,
_ => unreachable!(),
},
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}
}
}
pub mod part2 {
use super::{EnumIter, HandType, IntoEnumIterator};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, EnumIter)]
pub enum CamelCard {
J,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
T,
Q,
K,
A,
}
impl TryFrom<char> for CamelCard {
type Error = &'static str;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'J' => Ok(Self::J),
'2' => Ok(Self::Two),
'3' => Ok(Self::Three),
'4' => Ok(Self::Four),
'5' => Ok(Self::Five),
'6' => Ok(Self::Six),
'7' => Ok(Self::Seven),
'8' => Ok(Self::Eight),
'9' => Ok(Self::Nine),
'T' => Ok(Self::T),
'Q' => Ok(Self::Q),
'K' => Ok(Self::K),
'A' => Ok(Self::A),
_ => Err("Invalid card"),
}
}
}
impl From<[CamelCard; 5]> for HandType {
fn from(value: [CamelCard; 5]) -> Self {
let mut hand_types_raw = smallvec::SmallVec::<[Self; 2]>::new();
let mut n_jokers = 0;
for card in CamelCard::iter() {
let n_cards = value.iter().filter(|&&c| c == card).count();
if card == CamelCard::J {
n_jokers = n_cards;
}
match n_cards {
2 => hand_types_raw.push(Self::OnePair),
3 => hand_types_raw.push(Self::ThreeOfKind),
4 => {
hand_types_raw.push(Self::FourOfKind);
break;
}
5 => {
hand_types_raw.push(Self::FiveOfKind);
break;
}
_ => {}
}
}
let hand_type = match hand_types_raw.len() {
0 => Self::HighCard,
1 => hand_types_raw[0],
2 => {
hand_types_raw.sort();
match hand_types_raw[0] {
Self::OnePair => match hand_types_raw[1] {
Self::OnePair => Self::TwoPair,
Self::ThreeOfKind => Self::FullHouse,
_ => unreachable!(),
},
_ => unreachable!(),
}
}
_ => unreachable!(),
};
match (hand_type, n_jokers) {
(Self::FourOfKind, 1 | 4) | (Self::FullHouse, 2 | 3) => Self::FiveOfKind,
(Self::FullHouse, 1) | (Self::ThreeOfKind, 1 | 3) | (Self::TwoPair, 2) => {
Self::FourOfKind
}
(Self::TwoPair, 1) => Self::FullHouse,
(Self::OnePair, 1 | 2) => Self::ThreeOfKind,
(Self::HighCard, 1) => Self::OnePair,
(hand, _) => hand,
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {"
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483
"};
#[test]
pub fn part1_example() {
assert_eq!(part1(SAMPLE), 6440);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE), 5905);
}
}