diff options
author | alyx <alyx@aleteoryx.me> | 2023-11-19 14:55:43 -0500 |
---|---|---|
committer | alyx <alyx@aleteoryx.me> | 2023-11-19 14:55:43 -0500 |
commit | 2d12f6ec2294e36ae090fdd1e10025452ec1a480 (patch) | |
tree | 9467466f8fb7daba9d76a6a956931eba51b5d865 /2015/rs/src/four.rs | |
download | adventofcode-2d12f6ec2294e36ae090fdd1e10025452ec1a480.tar.gz adventofcode-2d12f6ec2294e36ae090fdd1e10025452ec1a480.tar.bz2 adventofcode-2d12f6ec2294e36ae090fdd1e10025452ec1a480.zip |
Initial commit, done up to 2015.7
Diffstat (limited to '2015/rs/src/four.rs')
-rw-r--r-- | 2015/rs/src/four.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/2015/rs/src/four.rs b/2015/rs/src/four.rs new file mode 100644 index 0000000..177ddc5 --- /dev/null +++ b/2015/rs/src/four.rs @@ -0,0 +1,46 @@ +use md5::{Digest, Md5}; +use std::fmt::Write; +use std::io::Write as WriteI; + +static INPUT: &'static [u8] = b"yzbqklnj"; + +fn main() { + let mut buf = Vec::with_capacity(INPUT.len() + 10); + buf.extend_from_slice(INPUT); + + for i in 0.. { + if i % 1000 == 0 { print!("{i}\r"); std::io::stdout().flush(); } + + let mut hasher = Md5::new(); + write!(&mut buf, "{i}"); + hasher.update(buf.as_slice()); + let hash = hasher.finalize(); + if hash.starts_with(&[0, 0]) && hash[2] < 16 { + let mut hexhash = String::with_capacity(32); + for digit in hash { + write!(&mut hexhash, "{digit:02x}"); + } + println!("Got 5 digit hash: {hexhash}, at {i}"); + break; + } + buf.truncate(INPUT.len()); + } + + for i in 0.. { + if i % 1000 == 0 { print!("{i}\r"); std::io::stdout().flush(); } + + let mut hasher = Md5::new(); + write!(&mut buf, "{i}"); + hasher.update(buf.as_slice()); + let hash = hasher.finalize(); + if hash.starts_with(&[0, 0, 0]) { + let mut hexhash = String::with_capacity(32); + for digit in hash { + write!(&mut hexhash, "{digit:02x}"); + } + println!("Got 6 digit hash: {hexhash}, at {i}"); + break; + } + buf.truncate(INPUT.len()); + } +} |