2015 day 4

This commit is contained in:
dece 2020-12-27 20:50:29 +01:00
parent 7f9920447a
commit a903132905
3 changed files with 40 additions and 0 deletions

9
2015/Cargo.lock generated
View file

@ -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"

View file

@ -8,3 +8,4 @@ edition = "2018"
name = "aoc"
[dependencies]
md5 = "0.7.0"

30
2015/src/bin/day4.rs Normal file
View file

@ -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);
}