generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.rs
190 lines (164 loc) · 5.36 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
use itertools::Itertools;
advent_of_code::solution!(7);
#[derive(Copy, Clone, Eq, PartialEq)]
enum JCardType {
Jack,
Joker,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Ord, PartialOrd)]
struct Card(u32);
impl Card {
fn new(c: char, j_card_type: JCardType) -> Self {
Self(match c {
'A' => 14,
'K' => 13,
'Q' => 12,
'J' => match j_card_type {
JCardType::Jack => 11,
JCardType::Joker => 1,
},
'T' => 10,
'9' => 9,
'8' => 8,
'7' => 7,
'6' => 6,
'5' => 5,
'4' => 4,
'3' => 3,
'2' => 2,
_ => unreachable!(),
})
}
fn is_joker(&self) -> bool {
self.0 == 1
}
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
enum HandType {
FiveOfAKind,
FourOfAKind,
FullHouse,
ThreeOfAKind,
TwoPairs,
OnePair,
HighCard,
}
impl HandType {
fn value(&self) -> u32 {
match self {
HandType::FiveOfAKind => 6,
HandType::FourOfAKind => 5,
HandType::FullHouse => 4,
HandType::ThreeOfAKind => 3,
HandType::TwoPairs => 2,
HandType::OnePair => 1,
HandType::HighCard => 0,
}
}
}
#[derive(Eq, PartialEq, Debug, Ord, PartialOrd)]
struct Hand(u32);
impl Hand {
fn new(cards: [Card; 5], j_card_type: JCardType) -> Self {
let sorted_cards = {
let mut cards = cards;
cards.sort_unstable();
cards
};
let [a, b, c, d, e] = sorted_cards;
let hand_type = if a == b && b == c && c == d && d == e {
HandType::FiveOfAKind
} else if (a == b && b == c && c == d) || (b == c && c == d && d == e) {
HandType::FourOfAKind
} else if (a == b && b == c && d == e) || (a == b && c == d && d == e) {
HandType::FullHouse
} else if (a == b && b == c) || (b == c && c == d) || (c == d && d == e) {
HandType::ThreeOfAKind
} else if (a == b && c == d) || (a == b && d == e) || (b == c && d == e) {
HandType::TwoPairs
} else if a == b || b == c || c == d || d == e {
HandType::OnePair
} else {
HandType::HighCard
};
let hand_type = match j_card_type {
JCardType::Jack => hand_type,
JCardType::Joker => {
let num_jokers = sorted_cards
.into_iter()
.filter(|card| card.is_joker())
.count();
match (hand_type, num_jokers) {
(HandType::FiveOfAKind, _) => HandType::FiveOfAKind,
(HandType::FourOfAKind, 1) | (HandType::FourOfAKind, 4) => {
HandType::FiveOfAKind
}
(HandType::FourOfAKind, _) => HandType::FourOfAKind,
(HandType::FullHouse, 2) | (HandType::FullHouse, 3) => HandType::FiveOfAKind,
(HandType::FullHouse, _) => HandType::FullHouse,
(HandType::ThreeOfAKind, 3) => HandType::FiveOfAKind,
(HandType::ThreeOfAKind, 1) => HandType::FourOfAKind,
(HandType::ThreeOfAKind, _) => HandType::ThreeOfAKind,
(HandType::TwoPairs, 2) => HandType::FourOfAKind,
(HandType::TwoPairs, 1) => HandType::FullHouse,
(HandType::TwoPairs, _) => HandType::TwoPairs,
(HandType::OnePair, 2) | (HandType::OnePair, 1) => HandType::ThreeOfAKind,
(HandType::OnePair, _) => HandType::OnePair,
(HandType::HighCard, 1) => HandType::OnePair,
(HandType::HighCard, _) => HandType::HighCard,
}
}
};
let mut repr = 0;
// 4 bits for hand type
repr |= hand_type.value();
// 4 bits per card = 20 bits
for card in cards {
repr <<= 4;
repr |= card.0;
}
Self(repr)
}
}
fn parse_input_iter(input: &str) -> impl Iterator<Item = ([char; 5], u32)> + '_ {
input.lines().map(|line| {
let cards: [u8; 5] = line.as_bytes()[..5].try_into().unwrap();
let cards = cards.map(|c| c as char);
let bid = line[6..].parse().unwrap();
(cards, bid)
})
}
fn solve(input: &str, j_card_type: JCardType) -> Option<u32> {
let mut hands = parse_input_iter(input)
.map(|(cards, bid)| {
let cards = cards.map(|c| Card::new(c, j_card_type));
(Hand::new(cards, j_card_type), bid)
})
.collect::<Vec<_>>();
hands.sort_unstable();
hands
.into_iter()
.enumerate()
.map(|(rank, (_, bid))| bid * (rank as u32 + 1))
.sum1()
}
pub fn part_one(input: &str) -> Option<u32> {
solve(input, JCardType::Jack)
}
pub fn part_two(input: &str) -> Option<u32> {
solve(input, JCardType::Joker)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(6440));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(5905));
}
}