diff --git a/2015/11/src/main.rs b/2015/11/src/main.rs index 65c822f..485e122 100644 --- a/2015/11/src/main.rs +++ b/2015/11/src/main.rs @@ -1,16 +1,24 @@ +use std::collections::HashSet; + + fn main() { - println!("Hello, world!"); + let first = generate("hxbxwxba"); + println!("{}", first); + println!("{}", generate(&first)); } fn validate(input: &str) -> bool { - let count = input.chars() + let count = input + .chars() .collect::>() .windows(3) .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(); - if count <= 0 { + if count == 0 { return false; } @@ -20,13 +28,13 @@ fn validate(input: &str) -> bool { } } - let count = input.chars() - .collect::>() + let count = input.as_bytes() .windows(2) .filter(|x| x[0] == x[1]) - .count(); + .map(|x| x[0]) + .collect::>(); - if count < 2 { + if count.len() < 2 { return false; } @@ -34,15 +42,31 @@ fn validate(input: &str) -> bool { } 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)] mod tests { - use super::{validate, generate}; + use super::{generate, validate}; #[test] fn test_validate() {