From 0a7aaec503684e50bc80eccd9a0851d62f8663e3 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Wed, 8 Sep 2021 12:12:11 -0700 Subject: [PATCH] 2015-04, add md5 helper function --- .gitignore | 3 +- .idea/advent.iml | 2 - .idea/misc.xml | 4 -- .idea/modules.xml | 8 --- .idea/vcs.xml | 6 -- .idea/workspace.xml | 126 --------------------------------------- CMakeLists.txt | 12 ++-- README.md | 2 +- build.sh | 1 + include/advent_utility.h | 2 + src/2015/04.c | 43 ++++++++++++- src/advent_utility.c | 16 +++++ 12 files changed, 70 insertions(+), 155 deletions(-) delete mode 100644 .idea/advent.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 438252d..4917783 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,5 @@ input/ advent compile_commands.json .cache -cmake-build-debug -cmake-build-release build +.clangd diff --git a/.idea/advent.iml b/.idea/advent.iml deleted file mode 100644 index 6d70257..0000000 --- a/.idea/advent.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index f1c67df..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index af3e273..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 9661ac7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 26edda4..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,126 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1630619869539 - - - - - - - - - - - \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a2048c..254e52a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.10) set(CMAKE_C_STANDARD 99) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_BUILD_TYPE Debug) project(advent C) @@ -17,12 +19,12 @@ file(COPY input DESTINATION ${CMAKE_BINARY_DIR}) add_executable(advent ${SRC} ${SRC2015} ${SRC2016} ${SRC2017} ${SRC2018} ${SRC2019} ${SRC2020}) -if ((${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD") OR (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")) - message(WARNING "${CMAKE_SYSTEM_NAME}: Not linking libbsd") +if ((${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")) target_link_libraries(advent PRIVATE flint) + target_include_directories(advent PRIVATE include lib/libflint/include) else() - message(WARNING "${CMAKE_SYSTEM_NAME}: Linking libbsd") - target_link_libraries(advent PRIVATE bsd flint) + find_package(OpenSSL REQUIRED) + target_link_libraries(advent PRIVATE bsd flint ${OPENSSL_LIBRARIES}) + target_include_directories(advent PRIVATE include lib/libflint/include ${OpenSSL_INCLUDE_DIR}) endif() -target_include_directories(advent PRIVATE include lib/libflint/include) diff --git a/README.md b/README.md index 91da08e..9287730 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Build the project using Cmake: cmake .. make -The supplied `build.sh` script uses `scan-build` to build a compilation database. This is not neccesary for users. +The supplied `build.sh` script does the above for you. ## Inputs diff --git a/build.sh b/build.sh index 1ef627e..3d4f896 100755 --- a/build.sh +++ b/build.sh @@ -5,4 +5,5 @@ cd build cmake .. make cp advent .. +cp compile_commands.json .. cd .. diff --git a/include/advent_utility.h b/include/advent_utility.h index 4f83c8b..88dbb89 100644 --- a/include/advent_utility.h +++ b/include/advent_utility.h @@ -11,4 +11,6 @@ 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*); + #endif diff --git a/src/2015/04.c b/src/2015/04.c index 8652ca7..3e5b742 100644 --- a/src/2015/04.c +++ b/src/2015/04.c @@ -1,10 +1,51 @@ #include #include +#include #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); } diff --git a/src/advent_utility.c b/src/advent_utility.c index b78feb0..908bb64 100644 --- a/src/advent_utility.c +++ b/src/advent_utility.c @@ -1,4 +1,7 @@ #include +#include +#include +#include #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; +}