/
day7.rs
196 lines (168 loc) Β· 4.45 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
// revised per https://github.com/rust-tw/advent-of-code/pull/86
use std::{cmp::Ordering, collections::HashMap};
type Label = u8;
enum HandType {
FiveOfAKind,
FourOfAKind,
FullHouse,
ThreeOfAKind,
TwoPair,
OnePair,
HighCard,
}
impl HandType {
fn inject_joker(map: &mut HashMap<&u8, i32>) {
let jokers = *map.get(&1).unwrap_or(&0);
if jokers == 0 {
return;
}
map.remove(&1);
let (mut max_key, mut max_value) = (1, -1);
for (&k, v) in &mut *map {
if *v > max_value {
max_value = *v;
max_key = *k;
}
}
match map.get_mut(&max_key) {
Some(v) => *v += jokers,
None => {
map.insert(&1, jokers);
}
};
}
fn from(labels: &[Label]) -> Self {
let mut map = HashMap::new();
for label in labels {
*map.entry(label).or_insert(0) += 1;
}
Self::inject_joker(&mut map);
let mut values: Vec<_> = map.values().collect();
values.sort();
match values.len() {
1 => Self::FiveOfAKind,
4 => Self::OnePair,
5 => Self::HighCard,
2 => match values[0..2] {
[1, 4] => Self::FourOfAKind,
[2, 3] => Self::FullHouse,
_ => panic!("how is this possible? π"),
},
3 => match values[0..3] {
[1, 1, 3] => Self::ThreeOfAKind,
[1, 2, 2] => Self::TwoPair,
_ => panic!("how is this possible? π"),
},
_ => panic!("how is this possible? π"),
}
}
fn rank(&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,
}
}
}
struct Hand {
hand_type: HandType,
labels: Vec<u8>,
bid: u32,
}
impl Hand {
fn parse_label(ch: &char, use_joker: bool) -> Label {
match ch {
'A' => 14,
'K' => 13,
'Q' => 12,
'J' => {
if use_joker {
1
} else {
11
}
}
'T' => 10,
'2'..='9' => *ch as u8 - b'0',
_ => panic!("how is this possible? π"),
}
}
fn from(input: &str, use_joker: bool) -> Self {
let (labels, bid) = input.split_once(' ').unwrap();
let labels: Vec<u8> = labels
.chars()
.map(|ch| Self::parse_label(&ch, use_joker))
.collect();
let hand_type = HandType::from(&labels);
let bid = bid.parse::<u32>().unwrap();
Self {
labels,
hand_type,
bid,
}
}
}
impl PartialOrd for Hand {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Hand {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Ord for Hand {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(self.hand_type.rank(), &self.labels).cmp(&(other.hand_type.rank(), &other.labels))
}
}
impl Eq for Hand {}
pub fn solve_part1(input: &[&str]) -> u32 {
let mut hands: Vec<_> = input.iter().map(|line| Hand::from(line, false)).collect();
hands.sort();
hands
.iter()
.enumerate()
.map(|(i, hand)| (i + 1) as u32 * hand.bid)
.sum()
}
pub fn solve_part2(input: &[&str]) -> u32 {
let mut hands: Vec<_> = input.iter().map(|line| Hand::from(line, true)).collect();
hands.sort();
hands
.iter()
.enumerate()
.map(|(i, hand)| (i + 1) as u32 * hand.bid)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() {
let input = [
"32T3K 765",
"T55J5 684",
"KK677 28",
"KTJJT 220",
"QQQJA 483",
];
assert_eq!(solve_part1(&input), 6440);
}
#[test]
fn test_part2() {
let input = [
"32T3K 765",
"T55J5 684",
"KK677 28",
"KTJJT 220",
"QQQJA 483",
];
assert_eq!(solve_part2(&input), 5905);
}
}