diff --git a/Cargo.lock b/Cargo.lock index fec8700..c6db471 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 07bd15b..99c314c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/tasks/day04.rs b/src/tasks/day04.rs index c9aa457..c155cab 100644 --- a/src/tasks/day04.rs +++ b/src/tasks/day04.rs @@ -1,3 +1,4 @@ +use itertools::Itertools; use std::ops::RangeInclusive; pub fn run() { @@ -10,8 +11,8 @@ fn task1(range: &RangeInclusive) { 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) { .map(|v| format!("{}", v)) .filter(|s| { let s = format!("0{}0", s); - (0..5) - .map(|i| s[i..i + 4].chars().collect::>()) - .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);