From 8e1fcf87c46ae72acd639c73a37476c894d187f3 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Wed, 1 Dec 2021 08:24:26 +0100 Subject: [PATCH] 2021 day1/rust: add solution --- .gitignore | 1 + 2021/day1/day1_rs/Cargo.lock | 25 +++++++++++++++++++++++++ 2021/day1/day1_rs/Cargo.toml | 9 +++++++++ 2021/day1/day1_rs/src/main.rs | 24 ++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 2021/day1/day1_rs/Cargo.lock create mode 100644 2021/day1/day1_rs/Cargo.toml create mode 100644 2021/day1/day1_rs/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/2021/day1/day1_rs/Cargo.lock b/2021/day1/day1_rs/Cargo.lock new file mode 100644 index 0000000..f1c836f --- /dev/null +++ b/2021/day1/day1_rs/Cargo.lock @@ -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", +] diff --git a/2021/day1/day1_rs/Cargo.toml b/2021/day1/day1_rs/Cargo.toml new file mode 100644 index 0000000..348a8b1 --- /dev/null +++ b/2021/day1/day1_rs/Cargo.toml @@ -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" diff --git a/2021/day1/day1_rs/src/main.rs b/2021/day1/day1_rs/src/main.rs new file mode 100644 index 0000000..32e167e --- /dev/null +++ b/2021/day1/day1_rs/src/main.rs @@ -0,0 +1,24 @@ +use itertools::Itertools; +use std::io::{self, BufRead}; + +fn count_increasing(i: impl Iterator) -> usize { + i.tuple_windows().filter(|(x, y)| x < y).count() +} + +fn main() { + let numbers: Vec = 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() }) + ) + ); +}