-
Notifications
You must be signed in to change notification settings - Fork 4
/
day13_fast.rs
161 lines (156 loc) · 4.69 KB
/
day13_fast.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
use super::util::iter_chunks;
use std::cmp::Ordering;
use std::iter;
use std::marker::PhantomData;
use std::str::FromStr;
#[derive(Clone, Copy, Eq, PartialEq)]
enum Token<T> {
Open,
Close,
Int(T),
}
struct TokenIterator<I, T> {
chars: I,
buf: Option<char>,
_marker: PhantomData<T>,
}
impl<I: Iterator<Item = char>, T: FromStr> Iterator for TokenIterator<I, T> {
type Item = Token<T>;
fn next(&mut self) -> Option<Self::Item> {
while let Some(char) = self.buf.take().or_else(|| self.chars.next()) {
match char {
',' => continue,
'0'..='9' => {
let digits = iter::once(char)
.chain(iter::from_fn(|| {
self.buf = self.chars.next();
self.buf.filter(|c| ('0'..='9').contains(c))
}))
.collect::<String>();
return Some(Token::Int(digits.parse().ok()?));
}
'[' => return Some(Token::Open),
']' => return Some(Token::Close),
_ => break,
}
}
None
}
}
fn tokenize<I, T>(iter: I) -> TokenIterator<I, T> {
TokenIterator {
chars: iter,
buf: None,
_marker: PhantomData,
}
}
fn compare<T: Ord, I: Iterator<Item = Token<T>>, J: Iterator<Item = Token<T>>>(
mut lhs: I,
mut rhs: J,
) -> Ordering {
loop {
match (
lhs.next().unwrap_or(Token::Close),
rhs.next().unwrap_or(Token::Close),
) {
(Token::Close, Token::Close) => {}
(Token::Close, _) => return Ordering::Less,
(_, Token::Close) => return Ordering::Greater,
(Token::Int(a), Token::Int(b)) => {
let ordering = a.cmp(&b);
if ordering != Ordering::Equal {
return ordering;
}
}
(Token::Int(a), b) => {
let Some((n, b)) = iter::once(b)
.chain(rhs.by_ref())
.enumerate()
.find(|(_, b)| b != &Token::Open)
.and_then(|(n, b)| match b {
Token::Int(b) => Some((n, b)),
_ => None,
}) else { return Ordering::Greater };
let ordering = a.cmp(&b);
if ordering != Ordering::Equal {
return ordering;
}
for _ in 0..n {
if rhs.next() != Some(Token::Close) {
return Ordering::Less;
}
}
}
(a, Token::Int(b)) => {
let Some((n, a)) = iter::once(a)
.chain(lhs.by_ref())
.enumerate()
.find(|(_, a)| a != &Token::Open)
.and_then(|(n, a)| match a {
Token::Int(a) => Some((n, a)),
_ => None,
}) else { return Ordering::Less };
let ordering = a.cmp(&b);
if ordering != Ordering::Equal {
return ordering;
}
for _ in 0..n {
if lhs.next() != Some(Token::Close) {
return Ordering::Greater;
}
}
}
(a, b) => assert!(a == b),
}
}
}
pub fn part1<'a, I, S>(lines: I) -> usize
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
iter_chunks(lines.into_iter())
.enumerate()
.filter(|(_, [a, b, _])| {
compare(
tokenize::<_, usize>(a.as_ref().chars()),
tokenize(b.as_ref().chars()),
) <= Ordering::Equal
})
.map(|(i, _)| i + 1)
.sum()
}
pub fn part2<'a, I, S>(lines: I) -> usize
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let a = [
Token::Open,
Token::Open,
Token::Int(2usize),
Token::Close,
Token::Close,
];
let b = [
Token::Open,
Token::Open,
Token::Int(6usize),
Token::Close,
Token::Close,
];
let (x, y) = lines
.into_iter()
.map(|line| line.as_ref())
.filter(|line| !line.is_empty())
.fold((1, 1), |(x, y), line| {
if compare(tokenize(line.chars()), a.iter().copied()) < Ordering::Equal {
(x + 1, y)
} else if compare(tokenize(line.chars()), b.iter().copied()) < Ordering::Equal {
(x, y + 1)
} else {
(x, y)
}
});
x * (x + y)
}