-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.rs
88 lines (80 loc) · 2.77 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
#![feature(str_split_once)]
pub fn main() {
let (rules, msgs) = include_str!("../input.txt").split_once("\n\n").unwrap();
let mut rules: Vec<(usize, Rule)> = rules
.lines()
.map(|l| {
let (n, rule) = l.split_once(": ").unwrap();
(
n.parse().unwrap(),
if rule.starts_with('"') {
Rule::Lit(rule.chars().nth(1).unwrap() as u8)
} else {
let parts: Vec<_> = rule.splitn(2, '|').collect();
let a = parts[0]
.split_terminator(' ')
.filter(|n| !n.is_empty())
.map(|n| n.parse().unwrap())
.collect();
if parts.len() == 1 {
Rule::Seq(a)
} else {
let b = parts[1]
.split_terminator(' ')
.filter(|n| !n.is_empty())
.map(|n| n.parse().unwrap())
.collect();
Rule::SeqOr(a, b)
}
},
)
})
.collect();
rules.sort_unstable_by_key(|r| r.0);
let rules: Vec<_> = rules.into_iter().map(|r| r.1).collect();
// rules[8] = Rule::SeqOr(vec![42], vec![42, 8]);
// rules[11] = Rule::SeqOr(vec![42, 31], vec![42, 11, 31]);
println!(
"{}",
msgs.lines()
.filter(|msg| matches_42(msg.as_bytes(), &rules))
.count(),
);
}
enum Rule {
Lit(u8),
Seq(Vec<usize>),
SeqOr(Vec<usize>, Vec<usize>),
}
fn matches_42(msg: &[u8], rules: &[Rule]) -> bool {
(0..)
.try_fold(msg, |msg, depth| match matches(msg, 42, rules) {
Some(msg) if matches_31(depth, msg, rules) => Err(true),
Some(msg) => Ok(msg),
None => Err(false),
})
.err()
.unwrap()
}
fn matches_31(depth: usize, msg: &[u8], rules: &[Rule]) -> bool {
(0..depth)
.try_fold(msg, |msg, _| match matches(msg, 31, rules) {
Some(msg) if msg.is_empty() => Err(true),
Some(msg) => Ok(msg),
None => Err(false),
})
.err()
.unwrap_or(false)
}
fn matches<'a>(msg: &'a [u8], rule: usize, rules: &[Rule]) -> Option<&'a [u8]> {
match &rules[rule] {
Rule::Lit(_) if msg.is_empty() => None,
Rule::Lit(c) if &msg[0] == c => Some(&msg[1..]),
Rule::Lit(_) => None,
Rule::Seq(a) => a.into_iter().try_fold(msg, |m, &r| matches(m, r, rules)),
Rule::SeqOr(a, b) => a
.into_iter()
.try_fold(msg, |m, &r| matches(m, r, rules))
.or_else(|| b.into_iter().try_fold(msg, |m, &r| matches(m, r, rules))),
}
}