From 57d678de5caf762a0066478db305af7f9c85c58f Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Fri, 13 Oct 2023 14:27:00 +0200 Subject: [PATCH] Day 4 2015 advent of code --- advent_of_code/2015/4/Cargo.toml | 9 ++++++ advent_of_code/2015/4/src/main.rs | 47 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 advent_of_code/2015/4/Cargo.toml create mode 100644 advent_of_code/2015/4/src/main.rs diff --git a/advent_of_code/2015/4/Cargo.toml b/advent_of_code/2015/4/Cargo.toml new file mode 100644 index 0000000..04f29e4 --- /dev/null +++ b/advent_of_code/2015/4/Cargo.toml @@ -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" diff --git a/advent_of_code/2015/4/src/main.rs b/advent_of_code/2015/4/src/main.rs new file mode 100644 index 0000000..c630508 --- /dev/null +++ b/advent_of_code/2015/4/src/main.rs @@ -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); +}