day04 task 2
This commit is contained in:
@@ -3,6 +3,7 @@ use std::ops::RangeInclusive;
|
|||||||
pub fn run() {
|
pub fn run() {
|
||||||
let range = 109165..=576723;
|
let range = 109165..=576723;
|
||||||
task1(&range);
|
task1(&range);
|
||||||
|
task2(&range);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn task1(range: &RangeInclusive<i32>) {
|
fn task1(range: &RangeInclusive<i32>) {
|
||||||
@@ -15,3 +16,19 @@ fn task1(range: &RangeInclusive<i32>) {
|
|||||||
|
|
||||||
println!("Task 1: There are {} valid passwords in the range", count);
|
println!("Task 1: There are {} valid passwords in the range", count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn task2(range: &RangeInclusive<i32>) {
|
||||||
|
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::<Vec<_>>())
|
||||||
|
.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);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user