-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2016_05.rs
111 lines (94 loc) · 2.99 KB
/
solution_2016_05.rs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use advent_of_code_common::cryptography::md5_for_string;
fn solve_1(door_id: &str) -> String {
let mut result: String = String::new();
let mut n: u32 = 0;
while result.len() < 8 {
let s = format!("{door_id}{n}");
let hash = md5_for_string(&s);
if &hash[.. 5] == "00000" {
let picked_char = hash
.chars()
.nth(5)
.unwrap_or_else(|| panic!("Hash is too short: {hash}"));
result.push(picked_char);
println!("Hash of {s} is {hash}, result is {result}");
}
n += 1;
}
result
}
fn solve_2(door_id: &str) -> String {
const PLACEHOLDER: char = '_';
const REQUIRED_PREFIX: &str = "00000";
let mut result: String = String::from_utf8(vec![PLACEHOLDER as u8; 8])
.unwrap_or_else(|_| panic!("Failed to create empty string"));
let mut n: u32 = 0;
loop {
let s = format!("{door_id}{n}");
let hash = md5_for_string(&s);
if hash.starts_with(REQUIRED_PREFIX) {
let position_char = hash
.chars()
.nth(5)
.unwrap_or_else(|| panic!("Hash is too short: {hash}"));
let picked_char = hash
.chars()
.nth(6)
.unwrap_or_else(|| panic!("Hash is too short: {hash}"));
let position: usize = (position_char as u8 - b'0') as usize;
if position < result.len() {
if result
.chars()
.nth(position)
.unwrap_or_else(|| panic!("Result too short: {result}"))
== PLACEHOLDER
{
result.replace_range(position ..= position, &picked_char.to_string());
println!(
"{result}: Updated character - hash of {s} is {hash}, position char is {position_char}, picked char is {picked_char}"
);
} else {
println!("{result}: Ignoring duplicate update - hash of {s} is {hash}");
}
} else {
println!("{result}: Ignoring invalid position - hash of {s} is {hash}");
}
if !result.contains(PLACEHOLDER) {
return result;
}
}
n += 1;
}
}
const DATA: &str = "uqwqemis";
fn main() {
solve_2("abc");
let result_1 = solve_1(DATA);
println!("Part 1: {result_1}");
let result_2 = solve_2(DATA);
println!("Part 2: {result_2}");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_solve_1_test() {
assert_eq!(solve_1("abc"), "18f47a30");
}
#[test]
#[ignore]
fn test_solve_1_real() {
assert_eq!(solve_1(DATA), "1a3099aa");
}
#[test]
#[ignore]
fn test_solve_2_test() {
assert_eq!(solve_1("abc"), "05ace8e3");
}
#[test]
#[ignore]
fn test_solve_2_real() {
assert_eq!(solve_2(DATA), "694190cd");
}
}