-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
282 lines (252 loc) · 7.05 KB
/
main.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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use itertools::Itertools;
use std::str::FromStr;
#[allow(clippy::missing_panics_doc)]
pub fn main() {
let data = include_str!("input.txt");
let mut game = data.parse::<CamelCards>().unwrap();
println!("Part 1: {}", part_one(&mut game));
println!("Part 2: {}", part_two(&mut game));
}
fn part_one(game: &mut CamelCards) -> u32 {
game.score()
}
fn part_two(game: &mut CamelCards) -> u32 {
game.add_jokers();
game.score()
}
#[derive(Debug)]
struct CamelCards {
data: Vec<(Hand, u32)>,
}
impl CamelCards {
fn score(&mut self) -> u32 {
self.data.iter_mut().for_each(|h| h.0.score());
self.data.sort_unstable_by_key(|&(h, _)| h);
self.data
.iter()
.enumerate()
.map(|(i, &(_, b))| u32::try_from(i + 1).unwrap() * b)
.sum()
}
fn add_jokers(&mut self) {
self.data.iter_mut().for_each(|h| h.0.add_jokers());
}
}
impl FromStr for CamelCards {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut data = Vec::new();
for l in s.lines() {
let (hand, bid) = l
.split_once(' ')
.ok_or_else(|| format!("Cannot split line into the hand and bid: {l}"))?;
let hand = hand.parse::<Hand>()?;
let bid = bid
.parse::<u32>()
.map_err(|_| format!("Cannot convert the bid {bid} into a number"))?;
data.push((hand, bid));
}
Ok(Self { data })
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
struct Hand {
cards: [Card; 5],
category: HandType,
}
impl Hand {
fn score(&mut self) {
let jokers = self.cards.iter().filter(|&&c| c == Card::Joker).count();
let mut temp = self.cards;
temp.sort_unstable();
let counts = temp
.into_iter()
.filter(|&c| c != Card::Joker)
.dedup_with_count()
.map(|(i, _)| i)
.sorted_unstable_by_key(|&i| std::cmp::Reverse(i))
.collect_vec();
self.category = match (counts.first(), counts.get(1), jokers) {
(Some(5), None, 0)
| (Some(4), None, 1)
| (Some(3), None, 2)
| (Some(2), None, 3)
| (Some(1), None, 4)
| (None, None, 5) => HandType::FiveOfAKind,
(Some(4), Some(1), 0)
| (Some(3), Some(1), 1)
| (Some(2), Some(1), 2)
| (Some(1), Some(1), 3) => HandType::FourOfAKind,
(Some(3), Some(2), 0) | (Some(2), Some(2), 1) => HandType::FullHouse,
(Some(3), Some(1), 0) | (Some(2), Some(1), 1) | (Some(1), Some(1), 2) => {
HandType::ThreeOfAKind
}
(Some(2), Some(2), 0) => HandType::TwoPair,
(Some(2), Some(1), 0) | (Some(1), Some(1), 1) => HandType::OnePair,
(Some(1), Some(1), 0) => HandType::HighCard,
_ => unreachable!(),
}
}
fn add_jokers(&mut self) {
self.cards
.iter_mut()
.filter(|c| **c == Card::Jack)
.for_each(|c| *c = Card::Joker);
}
}
impl FromStr for Hand {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() != 5 {
return Err(format!(
"Hand is expected to be made of 5 cards exactly: {s}"
));
}
let mut cards = [Card::Two; 5];
for (i, c) in cards.iter_mut().enumerate() {
*c = s[i..=i].parse()?;
}
let output = Self {
cards,
category: HandType::HighCard,
};
Ok(output)
}
}
impl Ord for Hand {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.category
.cmp(&other.category)
.then(self.cards[0].cmp(&other.cards[0]))
.then(self.cards[1].cmp(&other.cards[1]))
.then(self.cards[2].cmp(&other.cards[2]))
.then(self.cards[3].cmp(&other.cards[3]))
.then(self.cards[4].cmp(&other.cards[4]))
}
}
impl PartialOrd for Hand {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HandType {
FiveOfAKind,
FourOfAKind,
FullHouse,
ThreeOfAKind,
TwoPair,
OnePair,
HighCard,
}
impl HandType {
const fn rank(self) -> u8 {
match self {
Self::FiveOfAKind => 7,
Self::FourOfAKind => 6,
Self::FullHouse => 5,
Self::ThreeOfAKind => 4,
Self::TwoPair => 3,
Self::OnePair => 2,
Self::HighCard => 1,
}
}
}
impl Ord for HandType {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.rank().cmp(&other.rank())
}
}
impl PartialOrd for HandType {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Card {
Ace,
King,
Queen,
Jack,
Ten,
Nine,
Eight,
Seven,
Six,
Five,
Four,
Three,
Two,
Joker,
}
impl Card {
const fn rank(self) -> u8 {
match self {
Self::Ace => 14,
Self::King => 13,
Self::Queen => 12,
Self::Jack => 11,
Self::Ten => 10,
Self::Nine => 9,
Self::Eight => 8,
Self::Seven => 7,
Self::Six => 6,
Self::Five => 5,
Self::Four => 4,
Self::Three => 3,
Self::Two => 2,
Self::Joker => 1,
}
}
}
impl FromStr for Card {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() > 1 {
return Err(format!("Card {s} is expected to only be one character"));
}
match s {
"A" => Ok(Self::Ace),
"K" => Ok(Self::King),
"Q" => Ok(Self::Queen),
"J" => Ok(Self::Jack),
"T" => Ok(Self::Ten),
"9" => Ok(Self::Nine),
"8" => Ok(Self::Eight),
"7" => Ok(Self::Seven),
"6" => Ok(Self::Six),
"5" => Ok(Self::Five),
"4" => Ok(Self::Four),
"3" => Ok(Self::Three),
"2" => Ok(Self::Two),
_ => Err(format!("Card {s} is not recognised")),
}
}
}
impl Ord for Card {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.rank().cmp(&other.rank())
}
}
impl PartialOrd for Card {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
let mut game = data.parse::<CamelCards>().unwrap();
assert_eq!(6440, part_one(&mut game));
}
#[test]
fn two() {
let data = include_str!("test.txt");
let mut game = data.parse::<CamelCards>().unwrap();
assert_eq!(5905, part_two(&mut game));
}
}