Day2
This commit is contained in:
48
day2/rust/src/main.rs
Normal file
48
day2/rust/src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user