59 lines
1.3 KiB
Rust
59 lines
1.3 KiB
Rust
|
use std::u64;
|
||
|
|
||
|
|
||
|
fn is_increasing(levels: &Vec<u64>) -> bool {
|
||
|
let mut current = 0;
|
||
|
for item in levels {
|
||
|
if *item < current {
|
||
|
return false;
|
||
|
} else {
|
||
|
current = *item;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
fn is_decreasing(levels: &Vec<u64>) -> bool {
|
||
|
let mut current = u64::MAX;
|
||
|
for item in levels {
|
||
|
if *item > current {
|
||
|
return false;
|
||
|
} else {
|
||
|
current = *item;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
fn is_monotonous(levels: &Vec<u64>) -> bool {
|
||
|
return is_increasing(levels) || is_decreasing(levels);
|
||
|
}
|
||
|
|
||
|
fn is_safe(levels: &Vec<u64>) -> bool {
|
||
|
if !is_monotonous(levels) {
|
||
|
return false;
|
||
|
}
|
||
|
for i in 0..(levels.len() - 1) {
|
||
|
let j = i+1;
|
||
|
let a = levels[i];
|
||
|
let b = levels[j];
|
||
|
let diff = (a as i64 - b as i64).abs() as u64;
|
||
|
if diff < 1 || diff > 3 {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
fn main() -> std::io::Result<()> {
|
||
|
let mut count = 0;
|
||
|
for line in std::io::stdin().lines() {
|
||
|
let line = line.unwrap();
|
||
|
let levels: Vec<u64> = line.split(" ").map(|x| x.parse::<u64>().unwrap()).collect();
|
||
|
if is_safe(&levels) {
|
||
|
count += 1;
|
||
|
}
|
||
|
}
|
||
|
println!("{}", count);
|
||
|
Ok(())
|
||
|
}
|