1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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");
}
}
|