From 16ef84a36b5c837a5f3c3f740882e330f45c4c4b Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 4 Dec 2019 21:03:14 +0100 Subject: [PATCH] day04 task 2 --- src/tasks/day04.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/tasks/day04.rs b/src/tasks/day04.rs index 0099807..c9aa457 100644 --- a/src/tasks/day04.rs +++ b/src/tasks/day04.rs @@ -3,6 +3,7 @@ use std::ops::RangeInclusive; pub fn run() { let range = 109165..=576723; task1(&range); + task2(&range); } fn task1(range: &RangeInclusive) { @@ -15,3 +16,19 @@ fn task1(range: &RangeInclusive) { println!("Task 1: There are {} valid passwords in the range", count); } + +fn task2(range: &RangeInclusive) { + let count = range + .clone() + .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]) + }) + .filter(|s| s.chars().zip(s.chars().skip(1)).all(|(a, b)| a <= b)) + .count(); + + println!("Task 1: There are {} valid passwords in the range", count); +}