-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.rs
189 lines (169 loc) · 5.07 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
use std::cmp::Ordering;
use aoc_lib::{answer::Answer, solution::Solution};
use itertools::Itertools;
pub struct Day7;
const CARDS_ORDER_A: [char; 13] = [
'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A',
];
const CARDS_ORDER_B: [char; 13] = [
'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A',
];
impl Solution for Day7 {
fn part_a(&self, input: &[String]) -> Answer {
let mut parsed = parse(input, CardsOrder::OrderA);
parsed.hands.sort_by(|a, b| {
a.to_hand_type_a()
.cmp(&b.to_hand_type_a())
.then_with(|| b.compare(a))
});
parsed
.hands
.iter()
.rev()
.enumerate()
.map(|(i, hand)| (i + 1) as u32 * hand.bid)
.sum::<u32>()
.into()
}
fn part_b(&self, input: &[String]) -> Answer {
let mut parsed = parse(input, CardsOrder::OrderB);
parsed.hands.sort_by(|a, b| {
a.to_hand_type_b()
.cmp(&b.to_hand_type_b())
.then_with(|| b.compare(a))
});
parsed
.hands
.iter()
.rev()
.enumerate()
.map(|(i, hand)| (i + 1) as u32 * hand.bid)
.sum::<u32>()
.into()
}
}
struct Parsed {
hands: Vec<Hand>,
}
#[derive(Debug)]
struct Hand {
cards: Vec<u8>,
bid: u32,
}
fn parse(input: &[String], card_order: CardsOrder) -> Parsed {
let mut hands = vec![];
for line in input {
let (cards, bid) = line.split_once(' ').unwrap();
let cards = cards
.chars()
.map(|card| {
let order = match card_order {
CardsOrder::OrderA => &CARDS_ORDER_A,
CardsOrder::OrderB => &CARDS_ORDER_B,
};
order.iter().position(|&ch| ch == card).unwrap() as u8
})
.collect();
let bid = bid.parse::<u32>().unwrap();
hands.push(Hand { cards, bid });
}
Parsed { hands }
}
impl Hand {
fn compare(&self, other: &Hand) -> Ordering {
for (&a, &b) in self.cards.iter().zip(&other.cards) {
if a != b {
return a.cmp(&b);
}
}
Ordering::Equal
}
fn to_hand_type_a(&self) -> HandType {
let mut count = [0; 13];
for card in self.cards.iter() {
count[*card as usize] += 1;
}
if count.iter().any(|&card| card == 5) {
HandType::FiveOfAKind
} else if count.iter().any(|&card| card == 4) {
HandType::FourOfAKind
} else if count.iter().any(|&card| card == 3) && count.iter().any(|&card| card == 2) {
HandType::FullHouse
} else if count.iter().any(|&card| card == 3) {
HandType::ThreeOfAKind
} else if count.iter().filter(|&&card| card == 2).count() == 2 {
HandType::TwoPair
} else if count.iter().any(|&card| card == 2) {
HandType::OnePair
} else {
HandType::HighCard
}
}
fn to_hand_type_b(&self) -> HandType {
let mut count = [0; 13];
for card in self.cards.iter() {
count[*card as usize] += 1;
}
let joker_count = count[0];
let sorted = count[1..13]
.iter()
.copied()
.filter(|&x| x != 0)
.sorted()
.rev()
.collect::<Vec<_>>();
// if full joker, then sorted len is 0
if sorted.is_empty() || joker_count + sorted[0] == 5 {
HandType::FiveOfAKind
} else if joker_count + sorted[0] == 4 {
HandType::FourOfAKind
} else if ((sorted[0] + joker_count == 3) && (sorted[1] == 2))
|| ((sorted[0] == 3) && (sorted[1] + joker_count == 2))
{
HandType::FullHouse
} else if sorted[0] + joker_count == 3 {
HandType::ThreeOfAKind
} else if ((sorted[0] + joker_count) == 2 && (sorted[1] == 2))
|| (sorted[0] == 2 && sorted[1] + joker_count == 2)
{
HandType::TwoPair
} else if sorted[0] + joker_count == 2 {
HandType::OnePair
} else {
HandType::HighCard
}
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord)]
enum HandType {
FiveOfAKind,
FourOfAKind,
FullHouse,
ThreeOfAKind,
TwoPair,
OnePair,
HighCard,
}
enum CardsOrder {
OrderA,
OrderB,
}
#[cfg(test)]
mod test {
use aoc_lib::{self, answer::Answer, input, solution::Solution};
use super::Day7;
#[test]
fn test_a() {
let input =
input::read_file(&format!("{}day_07_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day7.part_a(&input);
assert_eq!(<i32 as Into<Answer>>::into(6440), answer);
}
#[test]
fn test_b() {
let input =
input::read_file(&format!("{}day_07_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day7.part_b(&input);
assert_eq!(<i32 as Into<Answer>>::into(5905), answer);
}
}