-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.rs
204 lines (170 loc) · 4.68 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::cmp::{Ordering, Reverse};
use aoc_runner_derive::{aoc, aoc_generator};
use nom::{
character::complete::{one_of, space1},
combinator::map,
sequence::tuple,
IResult,
};
use crate::common::{
heap_retain,
nom::{nom_lines, nom_usize, process_input},
};
type HandValue = Reverse<u8>;
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub enum HandTypes {
Five,
Four,
Full,
Three,
TwoPair,
OnePair,
High,
}
#[derive(Debug, Clone)]
pub struct Hand {
cards: [HandValue; 5],
value: usize,
part1: HandTypes,
part2: HandTypes,
}
impl Hand {
fn classify(cards: &[HandValue; 5]) -> (HandTypes, HandTypes) {
let helper = |count, sec| match count {
5 => HandTypes::Five,
4 => HandTypes::Four,
3 if sec == 2 => HandTypes::Full,
3 => HandTypes::Three,
2 if sec == 2 => HandTypes::TwoPair,
2 => HandTypes::OnePair,
1 => HandTypes::High,
_ => panic!("unknown hand type {:?}", cards),
};
let mut counts = [0usize; 15];
for &Reverse(c) in cards {
if c == 11 {
counts[1] += 1;
} else {
counts[usize::from(c)] += 1;
}
}
let jokers = counts[1];
let [max, sec] = counts[2..]
.iter()
.fold([0, 0], |acc, &n| heap_retain::accumulate_max_n(acc, n));
let part2 = helper(max + jokers, sec);
let [max, sec] = heap_retain::accumulate_max_n([max, sec], jokers);
let part1 = helper(max, sec);
(part1, part2)
}
fn cards_compare<const JOKER: bool>(s: &[HandValue], other: &[HandValue]) -> Ordering {
for (a, b) in s.iter().zip(other.iter()) {
let a = if JOKER && a.0 == 11 { Reverse(1) } else { *a };
let b = if JOKER && b.0 == 11 { Reverse(1) } else { *b };
let c = a.cmp(&b);
if c != Ordering::Equal {
return c;
}
}
Ordering::Equal
}
fn compare<const JOKER: bool>(&self, other: &Self) -> Ordering {
let (o, s) = if JOKER {
(&other.part2, &self.part2)
} else {
(&other.part1, &self.part1)
};
o.cmp(s)
.then_with(|| Self::cards_compare::<JOKER>(&other.cards, &self.cards))
}
}
fn parse_card(s: &str) -> IResult<&str, HandValue> {
map(one_of("23456789TJQKA"), |c: char| {
Reverse(match c {
'2'..='9' => c as u8 - b'0',
'T' => 10,
'J' => 11,
'Q' => 12,
'K' => 13,
'A' => 14,
_ => panic!("unknown symbol"),
})
})(s)
}
fn parse_hand(s: &str) -> IResult<&str, [HandValue; 5]> {
map(
tuple((parse_card, parse_card, parse_card, parse_card, parse_card)),
|x| [x.0, x.1, x.2, x.3, x.4],
)(s)
}
fn parse_line(s: &str) -> IResult<&str, Hand> {
let (s, cards) = parse_hand(s)?;
let (s, _) = space1(s)?;
let (s, value) = nom_usize(s)?;
let (part1, part2) = Hand::classify(&cards);
Ok((
s,
Hand {
cards,
value,
part1,
part2,
},
))
}
#[aoc_generator(day7)]
fn generator(input: &str) -> Vec<Hand> {
process_input(nom_lines(parse_line))(input)
}
fn solve<const JOKER: bool>(inputs: &[Hand]) -> usize {
let mut inputs = inputs.to_vec();
inputs.sort_unstable_by(|a, b| a.compare::<JOKER>(b));
inputs
.into_iter()
.enumerate()
// .inspect(|x| println!("{x:?}"))
.map(|(place, hand)| hand.value * (place + 1))
.sum()
}
#[aoc(day7, part1)]
pub fn part1(inputs: &[Hand]) -> usize {
solve::<false>(inputs)
}
#[aoc(day7, part2)]
pub fn part2(inputs: &[Hand]) -> usize {
solve::<true>(inputs)
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = r"32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483";
#[test]
pub fn input_test() {
println!("{:?}", generator(SAMPLE));
// assert_eq!(generator(SAMPLE), Object());
}
#[test]
pub fn part1_test() {
assert_eq!(part1(&generator(SAMPLE)), 6440);
}
#[test]
pub fn part2_test() {
assert_eq!(part2(&generator(SAMPLE)), 5905);
}
mod regression {
use super::*;
const INPUT: &str = include_str!("../input/2023/day7.txt");
const ANSWERS: (usize, usize) = (251058093, 249781879);
#[test]
pub fn test() {
let input = INPUT.trim_end_matches('\n');
let output = generator(input);
assert_eq!(part1(&output), ANSWERS.0);
assert_eq!(part2(&output), ANSWERS.1);
}
}
}