2021 day1/rust: add solution
This commit is contained in:
parent
a29f0f3064
commit
a692d567c0
3 changed files with 58 additions and 0 deletions
25
2021/day1/day1_rs/Cargo.lock
generated
Normal file
25
2021/day1/day1_rs/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 = "day1_rs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
9
2021/day1/day1_rs/Cargo.toml
Normal file
9
2021/day1/day1_rs/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "day1_rs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.10.1"
|
24
2021/day1/day1_rs/src/main.rs
Normal file
24
2021/day1/day1_rs/src/main.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use itertools::Itertools;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn count_increasing(i: impl Iterator<Item = impl Ord + Clone>) -> usize {
|
||||
i.tuple_windows().filter(|(x, y)| x < y).count()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let numbers: Vec<usize> = io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.map(|l| str::parse(&l.unwrap()).unwrap())
|
||||
.collect();
|
||||
|
||||
println!("{}", count_increasing(numbers.iter()));
|
||||
println!(
|
||||
"{}",
|
||||
count_increasing(
|
||||
numbers
|
||||
.windows(3)
|
||||
.map(|window| -> usize { window.iter().sum() })
|
||||
)
|
||||
);
|
||||
}
|
Loading…
Reference in a new issue