-
Notifications
You must be signed in to change notification settings - Fork 0
/
d7.rs
178 lines (159 loc) · 4.6 KB
/
d7.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
#![allow(
clippy::must_use_candidate,
clippy::missing_panics_doc,
clippy::cast_possible_truncation
)]
use itertools::Itertools;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Card {
Joker,
N(u8),
T,
J,
Q,
K,
A,
}
impl Card {
fn from_char(c: char, with_joker: bool) -> Self {
match c {
'A' => Self::A,
'K' => Self::K,
'Q' => Self::Q,
'J' => {
if with_joker {
Self::Joker
} else {
Self::J
}
}
'T' => Self::T,
n => Self::N(n.to_digit(10).unwrap() as u8),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum HandType {
FiveOfAKind,
FourOfAKind,
FullHouse,
ThreeOfAKind,
TwoPair,
OnePair,
HighCard,
}
impl HandType {
fn from_cards(cards: &[Card]) -> Self {
match cards
.iter()
.counts()
.iter()
.map(|(k, v)| (v, k))
.sorted_unstable()
.rev()
.collect::<Vec<_>>()
.as_slice()
{
[(5, _)]
| [(4, Card::Joker), (1, _)]
| [(4, _), (1, Card::Joker)]
| [(3, _), (2, Card::Joker)]
| [(3, Card::Joker), (2, _)] => Self::FiveOfAKind,
[(4, _), (1, _)]
| [(3, Card::Joker), (1, _), (1, _)]
| [(3, _), (1, Card::Joker), (1, _)]
| [(3, _), (1, _), (1, Card::Joker)]
| [(2, Card::Joker), (2, _), (1, _)]
| [(2, _), (2, Card::Joker), (1, _)] => Self::FourOfAKind,
[(3, _), (2, _)] | [(2, _), (2, _), (_, Card::Joker)] => Self::FullHouse,
[(3, _), (_, _), (_, _)] | [(2, Card::Joker), (1, _), (1, _), (1, _)] => {
Self::ThreeOfAKind
}
cc @ [(2, _), (_, _), (_, _), (_, _)] if cc.contains(&(&1, &&Card::Joker)) => {
Self::ThreeOfAKind
}
[(2, _), (2, _), (_, _)] => Self::TwoPair,
[(2, _), (_, _), (_, _), (_, _)] => Self::OnePair,
cc if cc.contains(&(&1, &&Card::Joker)) => Self::OnePair,
[(1, _), (_, _), (_, _), (_, _), (_, _)] => Self::HighCard,
hand => panic!("Invalid hand {hand:?}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Hand {
cards: Vec<Card>,
hand_type: HandType,
}
impl Hand {
fn new(cards: Vec<Card>) -> Self {
let hand_type = HandType::from_cards(&cards);
Self { cards, hand_type }
}
}
impl PartialOrd for Hand {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Hand {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.hand_type.cmp(&other.hand_type) {
std::cmp::Ordering::Equal => {
for (c1, c2) in self.cards.iter().zip(other.cards.iter()) {
match c1.cmp(c2) {
std::cmp::Ordering::Equal => continue,
o => return o.reverse(),
}
}
std::cmp::Ordering::Equal
}
o => o,
}
}
}
pub fn part1(input: &str) -> usize {
input
.lines()
.map(|line| {
let (cards, bid) = line.split_once(' ').unwrap();
let bid = bid.parse::<usize>().unwrap();
let hand = Hand::new(
cards
.chars()
.map(|card| Card::from_char(card, false))
.collect(),
);
(hand, bid)
})
.sorted_unstable_by_key(|(hand, _)| hand.clone())
.rev()
.enumerate()
.map(|(rank, (_, bid))| (rank + 1) * bid)
.sum()
}
pub fn part2(input: &str) -> usize {
input
.lines()
.map(|line| {
let (cards, bid) = line.split_once(' ').unwrap();
let bid = bid.parse::<usize>().unwrap();
let hand = Hand::new(
cards
.chars()
.map(|card| Card::from_char(card, true))
.collect(),
);
(hand, bid)
})
.sorted_by_key(|(hand, _)| hand.clone())
.rev()
.enumerate()
.map(|(rank, (_, bid))| (rank + 1) * bid)
.sum()
}
pub fn main() {
let input = std::fs::read_to_string("../../input/7.txt").expect("Input file not found");
println!("Part 1 : {}", part1(&input));
println!("Part 2 : {}", part2(&input));
}