-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.rs
39 lines (36 loc) · 1.11 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
use regex::Regex;
pub fn main() {
let re = Regex::new(r#"^.*: (\d+)-(\d+) or (\d+)-(\d+)$"#).unwrap();
let data = include_str!("../input.txt");
let rules: Vec<(usize, usize, usize, usize)> = data
.lines()
.take_while(|l| !l.is_empty())
.map(|rule| {
let cap = re.captures(rule).unwrap();
(
cap[1].parse::<usize>().unwrap(),
cap[2].parse::<usize>().unwrap(),
cap[3].parse::<usize>().unwrap(),
cap[4].parse::<usize>().unwrap(),
)
})
.collect();
println!(
"{}",
data.lines()
.skip(rules.len() + 1)
.skip_while(|&t| t != "nearby tickets:")
.skip(1)
.flat_map(|ticket| {
ticket
.split(',')
.map(|field| field.parse::<usize>().unwrap())
})
.filter(|&f| {
!rules
.iter()
.any(|r| (f >= r.0 && f <= r.1) || (f >= r.2 && f <= r.3))
})
.sum::<usize>(),
);
}