Day2
This commit is contained in:
25
day2/rust/Cargo.lock
generated
Normal file
25
day2/rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,25 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
7
day2/rust/Cargo.toml
Normal file
7
day2/rust/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.13.0"
|
||||
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