From 3fdfd035b1f1713c33429434b467d379dbea94f8 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Fri, 2 Dec 2022 08:10:58 -0800 Subject: [PATCH] 2022-01 --- CMakeLists.txt | 7 ++----- lib/libflint | 2 +- src/2022/01.c | 41 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c0bad3e..c60228e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,10 +21,6 @@ file(COPY input DESTINATION ${CMAKE_BINARY_DIR}) add_executable(advent ${SRC} ${SRC2015} ${SRC2016} ${SRC2017} ${SRC2018} ${SRC2019} ${SRC2020} ${SRC2021} ${SRC2022}) -if ((${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")) - set(OPENSSL_ROOT_DIR /opt/homebrew/opt/openssl@3) -endif() - if ((${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")) target_link_libraries(advent PRIVATE flint) target_include_directories(advent PRIVATE include lib/libflint/include lib/uthash/src) @@ -33,10 +29,11 @@ elseif ((${CMAKE_SYSTEM_NAME} STREQUAL "Linux")) target_link_libraries(advent PRIVATE bsd flint ${OPENSSL_LIBRARIES}) target_include_directories(advent PRIVATE include lib/libflint/include lib/uthash/src ${OpenSSL_INCLUDE_DIR}) elseif ((${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")) + set(OPENSSL_ROOT_DIR /opt/homebrew/opt/openssl@3) set(OpenSSL_INCLUDE_DIR /opt/homebrew/opt/openssl@3/include) find_package(OpenSSL REQUIRED) target_link_libraries(advent PRIVATE flint ${OPENSSL_LIBRARIES}) target_include_directories(advent PRIVATE include lib/libflint/include lib/uthash/src ${OpenSSL_INCLUDE_DIR}) else() - message( FATAL_ERROR "OS ${CMAKE_SYSTEM_NAME} is not supported" ) + message(FATAL_ERROR "OS ${CMAKE_SYSTEM_NAME} is not supported" ) endif() diff --git a/lib/libflint b/lib/libflint index fc508da..25a8c50 160000 --- a/lib/libflint +++ b/lib/libflint @@ -1 +1 @@ -Subproject commit fc508da8fe7ce794ec7f9cdf274ca0ec0ec709b7 +Subproject commit 25a8c5004d7c213dc5a098625ab15a0435a90105 diff --git a/src/2022/01.c b/src/2022/01.c index b20b793..be9a6be 100644 --- a/src/2022/01.c +++ b/src/2022/01.c @@ -1,10 +1,45 @@ #include #include +#include +#include #include "lfinput.h" +#include "lflinkedlist.h" + +static int cmp(const void* a, const void* b) { + return (*(int*)a - *(int*)b); +} void advent2022day01(void) { - char *input = get_input("input/2022/01"); - printf("Solution for Day 01 of 2022 is not completed yet\n"); - free(input); + char *found, *input = get_input("input/2022/01"); + const char *errstr; + int t = 0; + + List* totals = malloc(sizeof(List)); + ll_init(totals, free); + + while ((found = strsep(&input, "\n")) != NULL) { + if (strcmp(found, "") == 0) { + int *i = malloc(sizeof(int)); + *i = t; + ll_ins_next(totals, totals->tail, (void*)i); + t = 0; + } else { + t += (int)(strtonum(found, 0, INT_MAX, &errstr)); + } + } + + int elves[totals->size]; + int i = 0; + LL_ITER(totals) { + elves[i++] = *(int*)(node->data); + } + + qsort(elves, totals->size, sizeof(int), cmp); + + printf("%d\n", elves[totals->size - 1]); + printf("%d\n", elves[totals->size - 1] + elves[totals->size - 2] + elves[totals->size - 3]); + + ll_destroy(totals); + free(input); }