day04 use itertools 'tuple_windows' instead of iter.zip(iter.skip(1))
This commit is contained in:
19
Cargo.lock
generated
19
Cargo.lock
generated
@@ -3,4 +3,23 @@
|
||||
[[package]]
|
||||
name = "aoc_2019"
|
||||
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"
|
||||
|
||||
@@ -7,3 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.8.2"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use itertools::Itertools;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
pub fn run() {
|
||||
@@ -10,8 +11,8 @@ fn task1(range: &RangeInclusive<i32>) {
|
||||
let count = range
|
||||
.clone()
|
||||
.map(|v| format!("{}", v))
|
||||
.filter(|s| s.chars().zip(s.chars().skip(1)).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().any(|(a, b)| a == b))
|
||||
.filter(|s| s.chars().tuple_windows().all(|(a, b)| a <= b))
|
||||
.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))
|
||||
.filter(|s| {
|
||||
let s = format!("0{}0", s);
|
||||
(0..5)
|
||||
.map(|i| s[i..i + 4].chars().collect::<Vec<_>>())
|
||||
.any(|sub| sub[0] != sub[1] && sub[1] == sub[2] && sub[2] != sub[3])
|
||||
s.chars()
|
||||
.tuple_windows()
|
||||
.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();
|
||||
|
||||
println!("Task 1: There are {} valid passwords in the range", count);
|
||||
|
||||
Reference in New Issue
Block a user