From a903132905e8711c637f3d0bf9e8bafce484562f Mon Sep 17 00:00:00 2001 From: dece Date: Sun, 27 Dec 2020 20:50:29 +0100 Subject: [PATCH] 2015 day 4 --- 2015/Cargo.lock | 9 +++++++++ 2015/Cargo.toml | 1 + 2015/src/bin/day4.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 2015/src/bin/day4.rs diff --git a/2015/Cargo.lock b/2015/Cargo.lock index 6a45c5d..5889f29 100644 --- a/2015/Cargo.lock +++ b/2015/Cargo.lock @@ -3,3 +3,12 @@ [[package]] name = "aoc2015" version = "0.1.0" +dependencies = [ + "md5", +] + +[[package]] +name = "md5" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" diff --git a/2015/Cargo.toml b/2015/Cargo.toml index 894e7fa..b0adb2c 100644 --- a/2015/Cargo.toml +++ b/2015/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" name = "aoc" [dependencies] +md5 = "0.7.0" diff --git a/2015/src/bin/day4.rs b/2015/src/bin/day4.rs new file mode 100644 index 0000000..8fca31d --- /dev/null +++ b/2015/src/bin/day4.rs @@ -0,0 +1,30 @@ +use aoc::input; + +fn main() { + let lines = input::read_lines(); + let mut key = lines[0].to_string(); + let key_len = key.len(); + + // Part 1 + let mut n = 0; + loop { + key.replace_range(key_len.., &n.to_string()); + let hash = md5::compute(key.as_bytes()); + if &hash[..2] == [0, 0] && hash[2] < 0x10 { + break + } + n += 1 + } + println!("Found coin with n {}.", n); + + // Part 2 + loop { + key.replace_range(key_len.., &n.to_string()); + let hash = md5::compute(key.as_bytes()); + if &hash[..3] == [0, 0, 0] { + break + } + n += 1 + } + println!("Found coin with n {}.", n); +}