Day 4 2015 advent of code
This commit is contained in:
9
advent_of_code/2015/4/Cargo.toml
Normal file
9
advent_of_code/2015/4/Cargo.toml
Normal 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"
|
||||||
47
advent_of_code/2015/4/src/main.rs
Normal file
47
advent_of_code/2015/4/src/main.rs
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user