diff options
author | Prefetch | 2023-02-25 11:41:27 +0100 |
---|---|---|
committer | Prefetch | 2023-02-25 11:41:27 +0100 |
commit | 3b877bf4cc667eb8bcc787d145203600a4dba2d2 (patch) | |
tree | c1d247def29fcb58ae28e4ae4e4d73d1b4e1b27f /d05/src |
Initial commit
Diffstat (limited to 'd05/src')
-rw-r--r-- | d05/src/main.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/d05/src/main.rs b/d05/src/main.rs new file mode 100644 index 0000000..25986ef --- /dev/null +++ b/d05/src/main.rs @@ -0,0 +1,86 @@ +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"); + } +} |