49 lines
1.1 KiB
Rust
49 lines
1.1 KiB
Rust
use itertools::Itertools;
|
|
|
|
fn main() {
|
|
let stdin = std::io::stdin();
|
|
let mut input = Vec::new();
|
|
|
|
for line in stdin.lines() {
|
|
let line = line.unwrap();
|
|
|
|
let levels: Vec<_> = line
|
|
.split_whitespace()
|
|
.map(|e| e.parse::<isize>().unwrap())
|
|
.collect();
|
|
|
|
input.push(levels);
|
|
}
|
|
|
|
println!("{}", input.iter().filter(|e| is_safe(e)).count());
|
|
|
|
println!(
|
|
"{}",
|
|
input
|
|
.iter()
|
|
.map(|levels| {
|
|
levels.iter().combinations(levels.len() - 1).any(|e| {
|
|
let combination: Vec<_> = e.into_iter().copied().collect();
|
|
is_safe(&combination)
|
|
})
|
|
})
|
|
.filter(|&e| e)
|
|
.count()
|
|
);
|
|
}
|
|
|
|
fn is_safe(levels: &Vec<isize>) -> bool {
|
|
if levels.len() < 2 {
|
|
return true;
|
|
}
|
|
|
|
let direction = levels[1] - levels[0];
|
|
|
|
return levels
|
|
.iter()
|
|
.tuple_windows()
|
|
.filter(|&(a, b)| (b - a).abs() < 1 || (b - a).abs() > 3 || (b - a) * direction < 0)
|
|
.count()
|
|
== 0;
|
|
}
|