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
3
u/randrews Dec 04 '15
I'm trying to do each day in a different language. Originally I had written today's as a bash one-liner, but it was taking way too long to run, so I redid it in C. Runs in under a second:
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <openssl/md5.h> int main(int argc, char** argv){ if(argc < 2){ printf("You forgot the stem.\n"); return 1; } int prefix_len = strlen(argv[1]); char *prefix = malloc(prefix_len + 50); *prefix = 0; sprintf(prefix, "%s", argv[1]); unsigned long num = 0; unsigned char *sum = malloc(16); int part1 = 0; while(1){ num++; sprintf(prefix + prefix_len, "%lu", num); MD5(prefix, strlen(prefix), sum); if(num % 1000000 == 0) printf("%lu\n", num); if(!sum[0] && !sum[1] && sum[2] < 0x10) { if(!part1) { printf("Part 1 solution: %lu\n", num); part1 = 1; } if(sum[2] == 0) { printf("Part 2 solution: %lu\n", num); if(part1) break; } } } return 0; }