-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
61 lines (51 loc) · 1.25 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
use itertools::Itertools;
fn check(row: &Vec<i64>) -> bool {
check_wrapper(row.iter())
}
fn check_wrapper<'a, I>(iter: I) -> bool
where
I: Iterator<Item = &'a i64>,
{
let mut direction = 0;
for (x,y) in iter.tuple_windows() {
let d = x-y;
if direction >= 0 && d >= 1 && d <= 3 {
direction = 1
} else if direction <= 0 && d >= -3 && d <= -1 {
direction = -1
} else{
return false
}
}
true
}
fn main() {
let data: Vec<Vec<i64>> = include_str!("./input.txt")
.trim()
.lines()
.map(|line|
line
.split(" ")
.map(|x| x.parse().unwrap())
.collect()
)
.collect();
let part1 = data.iter()
.map(check)
.filter(|&x| x)
.count();
println!("part 1: {}", part1);
let part2 = data.iter()
.map(|row| {
for i in 0..row.len() {
let chain = row[0..i].iter().chain(row[i+1..row.len()].iter());
if check_wrapper(chain) {
return true
}
}
return false
})
.filter(|&x| x)
.count();
println!("part 2: {}", part2);
}