2015-04, add md5 helper function

This commit is contained in:
2021-09-08 12:12:11 -07:00
parent 2e89a903ec
commit 0a7aaec503
12 changed files with 70 additions and 155 deletions

View File

@ -1,10 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "input.h"
#include "advent_utility.h"
static int is_five(char *test) {
char *t = test;
for (int i = 0; i < 5; ++i, ++t) {
if (*t != '0') {
return 0;
}
}
return 1;
}
static int is_six(char *test) {
char *t = test;
for (int i = 0; i < 6; ++i, ++t) {
if (*t != '0') {
return 0;
}
}
return 1;
}
void advent2015day04(void) {
char *input = get_input("input/2015/04");
printf("Solution for Day 04 of 2015 is not completed yet\n");
int c = 0, five = 0, six = 0;
while (!five || !six) {
char test[strlen(input) + 8];
sprintf(test, "%s%d", input, c);
char *md5 = md5_str(test);
if (!five && is_five(md5)) {
five = c;
printf("%d\n", five);
}
if (is_six(md5)) {
six = c;
}
free(md5);
++c;
}
printf("%d\n", six);
free(input);
}

View File

@ -1,4 +1,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <md5.h>
#include "advent_utility.h"
@ -26,3 +29,16 @@ int same_Point(const struct Point* a, const struct Point* b) {
int same_Point_v(const void *a, const void *b) {
return same_Point((const struct Point *)a, (const struct Point *)b);
}
char *md5_str(const char* input) {
unsigned char digest[16];
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, input, strlen(input));
MD5Final(digest, &context);
char *md5string = malloc(33);
for(int i = 0; i < 16; ++i) {
sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
}
return md5string;
}