Day 4 2015 advent of code

This commit is contained in:
2023-10-13 14:27:00 +02:00
parent d644336030
commit 57d678de5c
2 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
[package]
name = "main"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
md5 = "0.7.0"

View File

@@ -0,0 +1,47 @@
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);
}