2015/11 done.

This commit is contained in:
2017-09-25 22:42:35 +02:00
parent 16636774b6
commit f16b76d841

View File

@@ -1,16 +1,24 @@
use std::collections::HashSet;
fn main() { fn main() {
println!("Hello, world!"); let first = generate("hxbxwxba");
println!("{}", first);
println!("{}", generate(&first));
} }
fn validate(input: &str) -> bool { fn validate(input: &str) -> bool {
let count = input.chars() let count = input
.chars()
.collect::<Vec<char>>() .collect::<Vec<char>>()
.windows(3) .windows(3)
.filter(|x| x[0] >= 'a' && x[2] <= 'z') .filter(|x| x[0] >= 'a' && x[2] <= 'z')
.filter(|x| (x[1] as i8 - x[0] as i8) == 1 && (x[2] as i8 - x[1] as i8) == 1) .filter(|x| {
(x[1] as i8 - x[0] as i8) == 1 && (x[2] as i8 - x[1] as i8) == 1
})
.count(); .count();
if count <= 0 { if count == 0 {
return false; return false;
} }
@@ -20,13 +28,13 @@ fn validate(input: &str) -> bool {
} }
} }
let count = input.chars() let count = input.as_bytes()
.collect::<Vec<char>>()
.windows(2) .windows(2)
.filter(|x| x[0] == x[1]) .filter(|x| x[0] == x[1])
.count(); .map(|x| x[0])
.collect::<HashSet<_>>();
if count < 2 { if count.len() < 2 {
return false; return false;
} }
@@ -34,15 +42,31 @@ fn validate(input: &str) -> bool {
} }
fn generate(input: &str) -> String { fn generate(input: &str) -> String {
let mut password = String::from(input); let mut password = input.as_bytes().to_vec();
password loop {
for i in (0..8).rev() {
password[i] += 1;
if password[i] > b'z' {
password[i] = b'a';
} else {
break;
}
}
if validate(&String::from_utf8_lossy(&password)) {
break;
}
}
String::from_utf8_lossy(&password).into()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{validate, generate}; use super::{generate, validate};
#[test] #[test]
fn test_validate() { fn test_validate() {