-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.rs
260 lines (231 loc) · 6.31 KB
/
day07.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
use std::fmt::Debug;
fn type_helper(first: u8, second: u8) -> u8 {
return match first {
5 => 6,
4 => 5,
3 => {
if second == 2 {
4
} else {
3
}
}
2 => {
if second == 2 {
2
} else {
1
}
}
1 => 0,
_ => unreachable!(),
};
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub(crate) struct Hand<T> {
pub(crate) cards: [T; 5],
pub(crate) bid: i32,
}
mod part1 {
use super::Hand;
use core::hash::Hash;
use std::{cmp::Ordering, collections::HashMap};
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Card {
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
Ace,
}
impl From<char> for Card {
fn from(value: char) -> Self {
return match value {
'A' => Self::Ace,
'K' => Self::King,
'Q' => Self::Queen,
'J' => Self::Jack,
'T' => Self::Ten,
'9' => Self::Nine,
'8' => Self::Eight,
'7' => Self::Seven,
'6' => Self::Six,
'5' => Self::Five,
'4' => Self::Four,
'3' => Self::Three,
'2' => Self::Two,
_ => unreachable!(),
};
}
}
fn hand_type(hand: &Hand<Card>) -> u8 {
let mut map: HashMap<Card, u8> = HashMap::new();
for card in &hand.cards {
map.insert(card.clone(), 1 + *map.get(card).unwrap_or(&0));
}
let mut amounts: Vec<&u8> = map.values().collect();
amounts.sort();
amounts.reverse();
return super::type_helper(*amounts[0], **amounts.get(1).unwrap_or(&&0_u8));
}
impl Ord for Hand<Card> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let type_order = hand_type(self).cmp(&hand_type(other));
if type_order != Ordering::Equal {
return type_order;
}
for i in 0..5 {
let card_order = self.cards[i].cmp(&other.cards[i]);
if card_order != Ordering::Equal {
return card_order;
}
}
return Ordering::Equal;
}
}
}
mod part2 {
use super::Hand;
use core::hash::Hash;
use std::{cmp::Ordering, collections::HashMap};
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Card {
Jack,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Queen,
King,
Ace,
}
impl From<char> for Card {
fn from(value: char) -> Self {
return match value {
'A' => Self::Ace,
'K' => Self::King,
'Q' => Self::Queen,
'J' => Self::Jack,
'T' => Self::Ten,
'9' => Self::Nine,
'8' => Self::Eight,
'7' => Self::Seven,
'6' => Self::Six,
'5' => Self::Five,
'4' => Self::Four,
'3' => Self::Three,
'2' => Self::Two,
_ => unreachable!(),
};
}
}
fn hand_type(hand: &Hand<Card>) -> u8 {
let mut map: HashMap<Card, u8> = HashMap::new();
for card in &hand.cards {
map.insert(card.clone(), 1 + *map.get(card).unwrap_or(&0));
}
let jacks = *map.get(&Card::Jack).unwrap_or(&0);
map.remove(&Card::Jack);
let mut amounts: Vec<&u8> = map.values().collect();
amounts.sort();
amounts.reverse();
if jacks == 5 {
return 6;
}
return super::type_helper(*amounts[0] + jacks, **amounts.get(1).unwrap_or(&&0_u8));
}
impl Ord for Hand<Card> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let type_order = hand_type(self).cmp(&hand_type(other));
if type_order != Ordering::Equal {
return type_order;
}
for i in 0..5 {
let card_order = self.cards[i].cmp(&other.cards[i]);
if card_order != Ordering::Equal {
return card_order;
}
}
return Ordering::Equal;
}
}
}
fn parser<T>(input: &str) -> Vec<Hand<T>>
where
T: Debug + From<char>,
{
return input
.lines()
.map(|line| Hand {
cards: line
.split_once(' ')
.unwrap()
.0
.chars()
.map(|c| T::from(c))
.collect::<Vec<T>>()
.try_into()
.unwrap(),
bid: line.split_once(' ').unwrap().1.parse().unwrap(),
})
.collect();
}
#[aoc_generator(day7, part1)]
fn parser_part1(input: &str) -> Vec<Hand<part1::Card>> {
return parser(input);
}
#[aoc_generator(day7, part2)]
fn parser_part2(input: &str) -> Vec<Hand<part2::Card>> {
return parser(input);
}
#[aoc(day7, part1)]
fn solver_part1(hands: &[Hand<part1::Card>]) -> i32 {
let mut hands: Vec<Hand<part1::Card>> = hands.to_vec();
hands.sort_by(Hand::cmp);
return hands
.iter()
.enumerate()
.map(|(n, hand)| ((n as i32) + 1) * hand.bid)
.sum();
}
#[aoc(day7, part2)]
fn solver_part2(hands: &[Hand<part2::Card>]) -> i32 {
let mut hands: Vec<Hand<part2::Card>> = hands.to_vec();
hands.sort_by(Hand::cmp);
return hands
.iter()
.enumerate()
.map(|(n, hand)| ((n as i32) + 1) * hand.bid)
.sum();
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE_1: &str = "32T3K 765\nT55J5 684\nKK677 28\nKTJJT 220\nQQQJA 483";
#[test]
fn test_parser_part1() {
let result = parser_part1(EXAMPLE_1);
println!("{:?}", result);
}
#[test]
fn test_solver_part1() {
assert_eq!(6440, solver_part1(&parser_part1(EXAMPLE_1)));
}
#[test]
fn test_solver_part2() {
assert_eq!(5905, solver_part2(&parser_part2(EXAMPLE_1)));
}
}