-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.rs
55 lines (48 loc) · 1.53 KB
/
day05.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
pub(crate) fn day05() {
println!("{}", part_a("uqwqemis"));
println!("{}", part_b("uqwqemis"));
}
fn part_a(door_id: &str) -> String {
let mut password = String::new();
let mut i = 0;
while password.len() < 8 {
let s = format!("{}{}", door_id, i);
let hash = format!("{:x}", md5::compute(s));
if hash.starts_with("00000") {
password.push(hash.chars().nth(5).unwrap());
}
i += 1;
}
password
}
fn part_b(door_id: &str) -> String {
let mut password = vec! {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
let mut i = 0;
while password.iter().filter(|c| **c == ' ').count() > 0 {
let s = format!("{}{}", door_id, i);
let hash = format!("{:x}", md5::compute(s));
if hash.starts_with("00000") {
let pos = hash.chars().nth(5).unwrap() as usize - '0' as usize;
if pos < 8 && password[pos] == ' ' {
let char = hash.chars().nth(6).unwrap();
password[pos] = char;
}
}
i += 1;
}
format!("{}{}{}{}{}{}{}{}", password[0], password[1], password[2], password[3], password[4], password[5], password[6], password[7])
}
#[cfg(test)]
mod day05_tests {
use crate::y2016::day05::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!("18f47a30", part_a("abc"));
assert_eq!("05ace8e3", part_b("abc"));
}
#[test]
fn input_works() {
assert_eq!("1a3099aa", part_a("uqwqemis"));
assert_eq!("694190cd", part_b("uqwqemis"));
}
}