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/ytYEHXHAxTTQGxa Dec 04 '15 edited Dec 04 '15

Similar with C and LibreSSL, but with everything hardcoded (endianness-dependent):

#include <openssl/md5.h>
#include <inttypes.h>
#include <stdio.h>

#define KEY "abcdef"
#define LEN 32

#define MASK 0x00f0ffff /* adjust for endianness and # of leading zeros */

int main()
{
        unsigned char hash[16], buf[LEN];
        long i = 1;

        do MD5(buf, snprintf(buf, LEN, KEY "%ld", ++i), hash);
        while(*(int32_t *) hash & MASK);
        
        printf("%ld\n", i);

        return 0;
}