use md5; fn solve_part1(seed: &str) -> String { let mut passw = String::new(); let mut i = 0; loop { let data = format!("{}{}", seed, i.to_string()); let hash = format!("{:x}", md5::compute(data)); // We've mined a block, so-- wait wrong software if hash.starts_with("00000") { passw.push(hash.chars().nth(5).unwrap()); if passw.len() >= 8 { break; } } i += 1; } passw } fn solve_part2(seed: &str) -> String { // We'll replace each `x' with the true character let mut passw = String::from("xxxxxxxx"); let mut i = 0; loop { let data = format!("{}{}", seed, i.to_string()); let hash = format!("{:x}", md5::compute(data)); // I wonder how this loop affects global power consumption... if hash.starts_with("00000") { let c = hash.chars().nth(5).unwrap(); if c.is_ascii_digit() { // Get index of character to replace let k = String::from(c).parse().unwrap(); // Is `k' in bounds, and haven't we replaced `passw[k]' already? if k < 8 && passw.chars().nth(k).unwrap() == 'x' { let s = String::from(hash.chars().nth(6).unwrap()); passw.replace_range(k..k + 1, &s); // Have we replaced all characters in `passw'? if !passw.contains('x') { break; } } } } i += 1; } passw } fn main() { // My personal input ID let id = "ugkcyxxp"; // Part 1 gives "d4cd2ee1" for me println!("Part 1 solution: {}", solve_part1(id)); // Part 2 gives "f2c730e5" for me println!("Part 2 solution: {}", solve_part2(id)); } #[cfg(test)] mod tests { use super::*; #[test] fn part1_example1() { let id = "abc"; assert_eq!(solve_part1(id), "18f47a30"); } #[test] fn part2_example1() { let id = "abc"; assert_eq!(solve_part2(id), "05ace8e3"); } }