day04 use itertools 'tuple_windows' instead of iter.zip(iter.skip(1))

This commit is contained in:
Johannes
2019-12-04 22:28:47 +01:00
parent 16ef84a36b
commit b61b382298
3 changed files with 27 additions and 6 deletions

19
Cargo.lock generated
View File

@@ -3,4 +3,23 @@
[[package]] [[package]]
name = "aoc_2019" name = "aoc_2019"
version = "0.1.0" version = "0.1.0"
dependencies = [
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "either"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "itertools"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"

View File

@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
itertools = "0.8.2"

View File

@@ -1,3 +1,4 @@
use itertools::Itertools;
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
pub fn run() { pub fn run() {
@@ -10,8 +11,8 @@ fn task1(range: &RangeInclusive<i32>) {
let count = range let count = range
.clone() .clone()
.map(|v| format!("{}", v)) .map(|v| format!("{}", v))
.filter(|s| s.chars().zip(s.chars().skip(1)).any(|(a, b)| a == b)) .filter(|s| s.chars().tuple_windows().any(|(a, b)| a == b))
.filter(|s| s.chars().zip(s.chars().skip(1)).all(|(a, b)| a <= b)) .filter(|s| s.chars().tuple_windows().all(|(a, b)| a <= b))
.count(); .count();
println!("Task 1: There are {} valid passwords in the range", count); println!("Task 1: There are {} valid passwords in the range", count);
@@ -23,11 +24,11 @@ fn task2(range: &RangeInclusive<i32>) {
.map(|v| format!("{}", v)) .map(|v| format!("{}", v))
.filter(|s| { .filter(|s| {
let s = format!("0{}0", s); let s = format!("0{}0", s);
(0..5) s.chars()
.map(|i| s[i..i + 4].chars().collect::<Vec<_>>()) .tuple_windows()
.any(|sub| sub[0] != sub[1] && sub[1] == sub[2] && sub[2] != sub[3]) .any(|(a, b, c, d)| a != b && b == c && c != d)
}) })
.filter(|s| s.chars().zip(s.chars().skip(1)).all(|(a, b)| a <= b)) .filter(|s| s.chars().tuple_windows().all(|(a, b)| a <= b))
.count(); .count();
println!("Task 1: There are {} valid passwords in the range", count); println!("Task 1: There are {} valid passwords in the range", count);