aoc/2016/src/bin/2016_05b.rs
2025-09-11 11:11:13 -04:00

50 lines
1.7 KiB
Rust

use std::io::{stdout, Write};
use md5::{Digest, Md5};
fn has_8_chars(input: &Vec<char>) -> bool {
let mut num_spaces = 0;
for ch in input {
if *ch == ' ' { num_spaces += 1 }
}
num_spaces == 0
}
fn display_current_password(input: &Vec<char>) {
print!("\x1b[2K\rPassword: ");
for char in input {
if *char == ' ' { print!("_"); } else { print!("{}", char) };
}
}
fn main() {
let input = "wtnhxymk";
let mut password = vec![' '; 8];
let mut last_password_displayed = password.clone();
for index in 2231253..=u32::MAX {
let to_hash = format!("{}{}", input, index);
let as_hash = format!("{:x}", Md5::digest(to_hash.as_bytes()));
if as_hash.starts_with("00000") {
// println!("Found hash with {index} -> {as_hash}");
let ( sixth,balance ) = as_hash.as_str().split_at(5).1.split_at(1);
let ( seventh, _) = balance.split_at(1);
stdout().flush().unwrap();
let index_for_new_char = sixth.parse::<u32>().unwrap_or(9);
if index_for_new_char < 9 && password[index_for_new_char as usize] == ' ' {
password[index_for_new_char as usize] = seventh.parse().unwrap();
}
let mut to_disp = String::new();
for char in &password {
to_disp.push(*char);
}
if password != last_password_displayed {
last_password_displayed = password.clone();
display_current_password(&last_password_displayed);
}
if has_8_chars(&password.clone()) { break }
}
}
println!("\nPassword is [{password:?}]");
}
// 437e60fc