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

View File

@@ -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);