diff --git a/.gitmodules b/.gitmodules index 7281ad5..0047079 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "lib/libflint"] path = lib/libflint - url = git@git.fputs.com:fputs/libflint + url = https://fputs.com/fputs/libflint diff --git a/include/advent_utility.h b/include/advent_utility.h index 88dbb89..e741d6d 100644 --- a/include/advent_utility.h +++ b/include/advent_utility.h @@ -7,10 +7,15 @@ struct Point { }; struct Point new_Point(int, int); -struct Point *new_Point_p(int, int); -int same_Point(const struct Point*, const struct Point*); -int same_Point_v(const void*, const void*); -char *md5_str(const char*); +struct Point *new_Point_p(int, int); + +int same_Point(const struct Point *, const struct Point *); + +int same_Point_v(const void *, const void *); + +char *md5_str(const char *); + +char *capture_system(const char *); #endif diff --git a/src/2015/05.c b/src/2015/05.c index c907452..79bea2a 100644 --- a/src/2015/05.c +++ b/src/2015/05.c @@ -1,10 +1,16 @@ #include #include -#include "input.h" +#include "advent_utility.h" void advent2015day05(void) { - char *input = get_input("input/2015/05"); - printf("Solution for Day 05 of 2015 is not completed yet\n"); - free(input); + /* Sometimes, grep really is the best tool */ + char *p1 = capture_system( + "grep -E \"(.*[aeiou]){3}\" ./input/2015/05 | grep \"\\(.\\)\\1\" | egrep -v \"(ab|cd|pq|xy)\" | wc -l"); + char *p2 = capture_system("grep \"\\(..\\).*\\1\" ./input/2015/05 | grep \"\\(.\\).\\1\" | wc -l"); + + printf("%s%s", p1, p2); + + free(p1); + free(p2); } diff --git a/src/advent_utility.c b/src/advent_utility.c index 908bb64..82f1d3c 100644 --- a/src/advent_utility.c +++ b/src/advent_utility.c @@ -1,7 +1,14 @@ #include #include #include + +#ifdef __linux__ + +#include + +#else #include +#endif #include "advent_utility.h" @@ -19,7 +26,7 @@ struct Point *new_Point_p(int x, int y) { return p; } -int same_Point(const struct Point* a, const struct Point* b) { +int same_Point(const struct Point *a, const struct Point *b) { if (a->x == b->x && a->y == b->y) { return 1; } @@ -27,18 +34,38 @@ 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); + 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; +char *md5_str(const char *input) { + unsigned char digest[16]; + +#ifdef __linux__ + MD5_CTX context; + MD5_Init(&context); + MD5_Update(&context, input, strlen(input)); + MD5_Final(digest, &context); +#else + struct MD5Context context; + MD5Init(&context); + MD5Update(&context, input, strlen(input)); + MD5Final(digest, &context); +#endif + + char *md5string = malloc(33); + for (int i = 0; i < 16; ++i) { + sprintf(&md5string[i * 2], "%02x", (unsigned int) digest[i]); + } + return md5string; +} + +char *capture_system(const char *cmd) { + char *buf = malloc(256); + FILE *tmp = popen(cmd, "r"); + if (tmp == NULL) { + printf("failed to open FILE *tmp\n"); + exit(1); + } + fgets(buf, 256, tmp); + return buf; }