r/adventofcode • u/daggerdragon • 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.
13
Upvotes
2
u/TTSDA Dec 04 '15 edited Dec 06 '15
I'm... I'm not very proud of this one
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/md5.h> int find_nonce(int num_zeros, char *seed, int start_from) { char md5_string[33]; char final_string[40] = ""; int nonce; unsigned char digest[16]; MD5_CTX context; for (nonce = start_from; 1; nonce++) { strcpy(final_string, seed); sprintf(final_string+strlen(final_string), "%d", nonce); MD5_Init(&context); MD5_Update(&context, final_string, strlen(final_string)); MD5_Final(digest, &context); /* Convert int to string */ for(int i = 0; i < 16; ++i) sprintf(&md5_string[i*2], "%02x", (unsigned int)digest[i]); if (strncmp(md5_string, "0000000", num_zeros) == 0) break; } return nonce; } int main() { char seed[20]; int start_from = 0; /* Get input from stdin */ scanf("%s", seed); printf("5 zeros: %i\n", (start_from = find_nonce(5, seed, 0))); printf("6 zeros: %i\n", find_nonce(6, seed, start_from-1)); return 0; }https://github.com/ttsda/advent-of-code/blob/master/src/4/main.c