-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
340 lines (281 loc) · 10.3 KB
/
main.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
test bench::bench_parsing ... bench: 172,231 ns/iter (+/- 2,498)
test bench::bench_part1 ... bench: 5,224,529 ns/iter (+/- 39,053)
test bench::bench_part2 ... bench: 369,039 ns/iter (+/- 4,438)
*/
// allow bench feature when using unstable flag
// use: $ rustup run nightly cargo bench --features unstable
#![cfg_attr(feature = "unstable", feature(test))]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
use aoc_import_magic::{import_magic, PuzzleOptions};
use regex::Regex;
use std::{
collections::{HashMap, HashSet},
io,
};
const DAY: u32 = 19;
type RuleSet = HashMap<usize, MatchingRule>;
type InputType = (RuleSet, Vec<String>);
type OutputType1 = usize;
type OutputType2 = OutputType1;
type TodaysPuzzleOptions = PuzzleOptions<InputType>;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
enum MatchingRule {
Letter(char),
Subgroups(Vec<Vec<usize>>),
Special(HashSet<String>),
}
const MAGIC_NUMBER: usize = 8;
impl MatchingRule {
fn full_match(&self, string: &str, ruleset: &RuleSet) -> bool {
match self.matches(string, ruleset) {
Some(len) => len == string.len(),
None => false,
}
}
fn matches(&self, string: &str, ruleset: &RuleSet) -> Option<usize> {
match self {
MatchingRule::Letter(cc) => {
match string.chars().nth(0) {
Some(first_cc) if *cc == first_cc => Some(1),
_ => None,
}
}
MatchingRule::Subgroups(groups) => {
let mut match_sizes = Vec::new();
for group in groups {
let mut offset = 0;
for subrule_num in group {
let subrule = ruleset.get(subrule_num).unwrap();
match subrule.matches(&string[offset..], ruleset) {
None => {
offset = 0;
break
},
Some(matching_chars) => offset += matching_chars,
}
}
match_sizes.push(offset);
}
match_sizes.into_iter().filter(|size| *size != 0).next()
}
MatchingRule::Special(matching_set) => {
if string.len() >= MAGIC_NUMBER && matching_set.contains(&string[0..MAGIC_NUMBER]) {
Some(MAGIC_NUMBER)
} else {
None
}
},
}
}
fn build_strings(&self, ruleset: &RuleSet) -> Vec<String> {
match self {
MatchingRule::Letter(cc) => vec![format!("{}", cc)],
MatchingRule::Subgroups(groups) => {
let mut strings = Vec::new();
for group in groups {
let mut group_strings = Vec::new();
for subrule_num in group {
let subrule = ruleset.get(subrule_num).unwrap();
let subrule_strings = subrule.build_strings(ruleset);
if group_strings.is_empty() {
group_strings = subrule_strings;
} else {
group_strings = group_strings.into_iter().map(|existing_group_string| {
subrule_strings.iter().map(|new_subrule_string| {
format!("{}{}", existing_group_string, new_subrule_string)
}).collect::<Vec<String>>()
}).flatten().collect();
}
}
strings.extend(group_strings);
}
strings
}
MatchingRule::Special(list) => list.iter().map(ToOwned::to_owned).collect(),
}
}
}
impl From<&str> for MatchingRule {
fn from(from: &str) -> MatchingRule {
if from.contains('"') {
MatchingRule::Letter(from.chars().nth(1).unwrap())
} else {
let groups = from.split("|").map(|group| {
group.split_ascii_whitespace().map(|item| item.parse().unwrap()).collect()
}).collect();
MatchingRule::Subgroups(groups)
}
}
}
fn parse_input(input: Vec<String>, _config: &HashMap<String, String>, _verbose: bool) -> InputType {
let mut rules = HashMap::new();
let mut strings = Vec::new();
for line in input {
lazy_static! {
static ref RE_RULE: Regex = Regex::new(
r"(?P<num>\d+): (?P<rule>.*)"
).unwrap();
}
match RE_RULE.captures(&line) {
Some(caps) => {
let rule = MatchingRule::from(&caps["rule"]);
let number = caps["num"].parse().unwrap();
rules.insert(number, rule);
}
None => {
if !line.is_empty() {
strings.push(line);
}
}
}
}
(rules, strings)
}
fn part1(po: &TodaysPuzzleOptions) -> OutputType1 {
let (rules, strings) = po.get_data();
let rule_0 = rules.get(&0).unwrap();
let mut count = 0;
for string in strings {
if rule_0.full_match(string, &rules) {
count += 1;
}
}
count
}
fn part2(po: &TodaysPuzzleOptions) -> OutputType2 {
let (rules, strings) = po.get_data();
let rules = rules.to_owned();
/*
rules.insert(8, MatchingRule::from("42 | 42 8"));
rules.insert(11, MatchingRule::from("42 31 | 42 11 31"));
rules.insert(31, MatchingRule::Special(
rules.get(&31).unwrap().build_strings(&rules).into_iter().collect()
));
rules.insert(42, MatchingRule::Special(
rules.get(&42).unwrap().build_strings(&rules).into_iter().collect()
));
*/
let strings_31: HashSet<_> = rules.get(&31).unwrap().build_strings(&rules).into_iter().collect();
let strings_42: HashSet<_> = rules.get(&42).unwrap().build_strings(&rules).into_iter().collect();
let mut count = 0;
for mut string in strings.clone() {
// MUST start with 42 TWICE
if string.len() < 2* MAGIC_NUMBER {
continue;
}
if !strings_42.contains(&string[0..MAGIC_NUMBER]) {
continue;
}
if !strings_42.contains(&string[MAGIC_NUMBER..MAGIC_NUMBER*2]) {
continue;
}
let mut counted_42s = 0;
let mut counted_31s = 0;
// now remove "42" as often as possible
while string.len() >= MAGIC_NUMBER && strings_42.contains(&string[0..MAGIC_NUMBER]) {
string = string.split_off(MAGIC_NUMBER);
counted_42s += 1;
}
// we need at least MAGIC_NUMBER more characters for at least one "31"
if string.len() < MAGIC_NUMBER {
continue;
}
// rmeove "31" as often as possible
while string.len() >= MAGIC_NUMBER && strings_31.contains(&string[0..MAGIC_NUMBER]) {
string = string.split_off(MAGIC_NUMBER);
counted_31s += 1;
}
// now the string must be empty AND we must have had more "42"s than "31"s (because each "31" requires EXACTLY
// one "42")
if string.len() == 0 && counted_42s > counted_31s {
count += 1;
}
}
println!("MAGIC NUMBER IS {}", MAGIC_NUMBER);
count
}
// =================================================================================================
// End of actual logic
// What follows is the main function glue as well as tests + benchmarking code
// =================================================================================================
fn main() -> Result<(), io::Error> {
println!("AoC 2020 | Day {}", DAY);
// This function is pure magic (see ../../aoc_import_magic/lib.rs) because it
// 1. parses command line arguments
// 2. reads the input file for the correct day
// 3. uses `parse_input` as a parsing function
// 4. returns a nice usable struct which contains everything which we need for the actual puzzle
let puzzle = import_magic(DAY, parse_input)?;
if !puzzle.skip_p1 {
let res1 = part1(&puzzle);
println!("Part 1 result: {}", res1);
};
let res2 = part2(&puzzle);
println!("Part 2 result: {}", res2);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use aoc_import_magic::{import_magic_with_params, PuzzleOptions};
pub(super) fn import_helper(inputname: &str) -> PuzzleOptions<InputType> {
let params = ["appname", "--input", inputname];
import_magic_with_params(DAY, parse_input, ¶ms).unwrap()
}
fn test_case_helper(inputname: &str, sol1: Option<OutputType1>, sol2: Option<OutputType2>) {
let po = import_helper(inputname);
if let Some(sol1) = sol1 {
let res1 = part1(&po);
assert_eq!(sol1, res1, "part1");
}
if let Some(sol2) = sol2 {
let res2 = part2(&po);
assert_eq!(sol2, res2, "part2");
}
}
#[test]
fn example_1() {
test_case_helper("example1", Some(2), None)
}
#[test]
fn example_2() {
test_case_helper("example2", Some(3), None)
}
}
#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;
use super::*;
use aoc_import_magic::test_helper_import_config;
use std::{
fs::File,
io::{BufRead, BufReader},
};
use test::Bencher;
fn helper_read_file(fname: &str) -> Vec<String> {
BufReader::new(File::open(fname).unwrap())
.lines()
.map(|line| line.unwrap())
.collect()
}
#[bench]
fn bench_parsing(bb: &mut Bencher) {
let input = helper_read_file(&format!("../../_inputs/day{:02}/real1.input", DAY));
let config = test_helper_import_config(DAY, "real1");
bb.iter(|| test::black_box(parse_input(input.to_owned(), &config, false)));
}
#[bench]
fn bench_part1(bb: &mut Bencher) {
let puzzle_options = tests::import_helper("real1");
bb.iter(|| test::black_box(part1(&puzzle_options)));
}
#[bench]
fn bench_part2(bb: &mut Bencher) {
let puzzle_options = tests::import_helper("real1");
bb.iter(|| test::black_box(part2(&puzzle_options)));
}
}