Files
contests/advent_of_code/2015/4/src/main.rs

48 lines
912 B
Rust

const INPUT: &str = "iwrupvqb";
fn solve(input: &str, desired_start: &str) -> u64 {
let mut postfix = 1;
loop {
let digest = md5::compute(format!("{input}{postfix}"));
let digest = format!("{:x?}", digest);
if digest.starts_with(desired_start) {
return postfix;
}
postfix += 1;
}
}
fn main() {
println!("Hello, this is Patrick!");
let first_result = solve(INPUT, "00000");
println!(
"Santa needs to use a safer hashing, but his result is: {}",
first_result
);
let second_result = solve(INPUT, "000000");
println!(
"The extra zero costs a lot and has the answer: {}",
second_result
);
}
#[test]
fn test_first1() {
let s = solve("abcdef", "00000");
assert_eq!(s, 609043);
}
#[test]
fn test_first2() {
let s = solve("pqrstuv", "00000");
assert_eq!(s, 1048970);
}