-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_07.rs
156 lines (132 loc) Β· 3.72 KB
/
day_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
use std::{cmp::Ordering, fmt::Debug};
use common::{solution, Answer};
use itertools::Itertools;
solution!("Camel Cards", 7);
fn part_a(input: &str) -> Answer {
let hands = parse(input, "AKQJT98765432");
solve(hands, Hand::score_a).into()
}
fn part_b(input: &str) -> Answer {
let hands = parse(input, "AKQT98765432J");
solve(hands, Hand::score_b).into()
}
fn solve(mut hands: Vec<Hand>, score: fn(&Hand) -> HandType) -> usize {
hands.sort_by(|a, b| score(a).cmp(&score(b)).then_with(|| b.score_first(a)));
hands
.iter()
.rev()
.enumerate()
.map(|(i, e)| e.bid as usize * (i + 1))
.sum::<usize>()
}
struct Hand {
cards: Vec<u8>,
bid: u16,
}
fn parse(input: &str, mappings: &'static str) -> Vec<Hand> {
let mut hands = Vec::new();
for line in input.lines() {
let (cards, bid) = line.split_at(5);
let cards = cards
.as_bytes()
.iter()
.map(|&c| 13 - mappings.find(c as char).unwrap() as u8)
.collect();
let bid = bid.trim().parse().unwrap();
hands.push(Hand { cards, bid });
}
hands
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
enum HandType {
FiveOfAKind,
FourOfAKind,
FullHouse,
ThreeOfAKind,
TwoPair,
OnePair,
HighCard,
}
impl Hand {
fn score_a(&self) -> HandType {
let mut counts = [0; 13];
for &c in &self.cards {
counts[13 - c as usize] += 1;
}
if counts.iter().any(|&c| c == 5) {
HandType::FiveOfAKind
} else if counts.iter().any(|&c| c == 4) {
HandType::FourOfAKind
} else if counts.iter().any(|&c| c == 3) && counts.iter().any(|&c| c == 2) {
HandType::FullHouse
} else if counts.iter().any(|&c| c == 3) {
HandType::ThreeOfAKind
} else if counts.iter().filter(|&&c| c == 2).count() == 2 {
HandType::TwoPair
} else if counts.iter().any(|&c| c == 2) {
HandType::OnePair
} else {
HandType::HighCard
}
}
fn score_b(&self) -> HandType {
let mut counts = [0; 13];
for &c in &self.cards {
counts[13 - c as usize] += 1;
}
let jacks = counts[12];
let counts = counts[0..12]
.iter()
.copied()
.filter(|x| *x != 0)
.sorted()
.rev()
.collect::<Vec<_>>();
if counts.len() <= 1 || counts[0] + jacks == 5 {
HandType::FiveOfAKind
} else if counts[0] + jacks == 4 {
HandType::FourOfAKind
} else if ((counts[0] + jacks == 3) && (counts[1] == 2))
|| ((counts[0] == 3) && (counts[1] + jacks == 2))
{
HandType::FullHouse
} else if counts[0] + jacks == 3 {
HandType::ThreeOfAKind
} else if (counts[0] + jacks == 2 && counts[1] == 2)
|| (counts[0] == 2 && counts[1] + jacks == 2)
{
HandType::TwoPair
} else if counts[0] + jacks == 2 {
HandType::OnePair
} else {
HandType::HighCard
}
}
fn score_first(&self, other: &Hand) -> Ordering {
for (&a, &b) in self.cards.iter().zip(other.cards.iter()) {
if a != b {
return a.cmp(&b);
}
}
Ordering::Equal
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 6440.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 5905.into());
}
}