r/adventofcode • • Dec 04 '15

SOLUTION MEGATHREAD --- Day 4 Solutions ---

--- Day 4: The Ideal Stocking Stuffer ---

Post your solution as a comment. Structure your post like the Day Three thread.

14 Upvotes

272 comments sorted by

View all comments

Show parent comment

3

u/Azdle Dec 04 '15

Here's my rust code:

extern crate crypto;

use crypto::digest::Digest;
use crypto::md5::Md5;
use std::fmt;

fn main() {
    let input = "bgvyzdsv";
    let mut counter = 0u64;

    let mut sh = Md5::new();

    loop {
        counter += 1;
        let test_key = fmt::format(format_args!("{}{}", input, counter));

        sh.input_str(&test_key);
        let hash_result = sh.result_str();

        if hash_result.starts_with("00000") {
            println!("{}: {}", test_key, hash_result);
        }
        if hash_result.starts_with("000000") {
            println!("{}: {}", test_key, hash_result);
            break;
        }

        sh.reset();
     }
}

Running that:

$ cargo build --release && time ./target/release/day4
   Compiling advent_2015 v0.1.0 (file:///Users/patrick/Documents/Asorted%20Things/Rust/advent_2015)
bgvyzds<redacted>: 000004b30d481662b9cb0c105f6549b2
bgvyzdsv<redacted>: 00000ee839d26e9ef816a3cbf879dadf
bgvyzdsv<redacted>: 000000b1b64bf5eb55aad89986126953
bgvyzdsv<redacted>: 000000b1b64bf5eb55aad89986126953
./target/release/day4  0.52s user 0.01s system 97% cpu 0.541 total

Not to bad.