diff --git a/.vscode/launch.json b/.vscode/launch.json index fd2cf57..f960f47 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "request": "launch", "name": "Launch", "program": "${workspaceFolder}/build/adventofcode/adventofcode", - "args": ["2024", "1", "1"], + "args": ["2024", "1", "2"], "cwd": "${workspaceFolder}", "preLaunchTask": "CMake: build", } diff --git a/2024/CMakeLists.txt b/2024/CMakeLists.txt index 5cc4b92..e28b2f6 100644 --- a/2024/CMakeLists.txt +++ b/2024/CMakeLists.txt @@ -13,6 +13,7 @@ endforeach() add_custom_target(prebuild_target DEPENDS ${adv_input}) add_dependencies(2024 prebuild_target) +target_link_libraries(2024 m) target_sources(2024 PRIVATE src/day1.c diff --git a/2024/src/day1.c b/2024/src/day1.c index 02f8321..02e209b 100644 --- a/2024/src/day1.c +++ b/2024/src/day1.c @@ -1,3 +1,116 @@ -void *day_1_part_1() { - return "yes"; +#include "include/days.h" +#include +#include +#include + +struct list_ctx { + u_int32_t* list; + size_t list_len; +}; +struct list_pair { + struct list_ctx *left; + struct list_ctx *right; +}; + +struct list_pair *parse_input() { + struct list_pair *ctx = malloc(sizeof(struct list_pair)); + ctx->left = malloc(sizeof(struct list_ctx)); + ctx->left->list = malloc(1000 * sizeof(u_int32_t)); + ctx->left->list_len = 1000; + ctx->right = malloc(sizeof(struct list_ctx)); + ctx->right->list = malloc(1000 * sizeof(u_int32_t)); + ctx->right->list_len = 1000; + + int list_i = 0; + int list_buf_len = 0; + char *list_buf = NULL; + for(int i = 0; i < input_day1_txt_len; i++) { + char c = input_day1_txt[i]; + switch(c) { + case ' ': + if (list_buf_len) { + ctx->left->list[list_i] = strtoul(list_buf, NULL, 10); + list_buf_len = 0; + list_buf = NULL; + }; + break; + case '\n': { + ctx->right->list[list_i] = strtoul(list_buf, NULL, 10); + list_i++; + list_buf_len = 0; + list_buf = NULL; + break; + } + default: { + list_buf_len++; + list_buf = realloc(list_buf, list_buf_len * sizeof(char)); + list_buf[list_buf_len-1] = c; + } + } + } + return ctx; +} + +void selection_sort(struct list_ctx *ctx) { + for(int start_i = 0; start_i < ctx->list_len; start_i++) { + size_t min_i = start_i; + for (size_t i = start_i + 1; i < ctx->list_len; i++) { + if (ctx->list[i] < ctx->list[min_i]) { + min_i = i; + } + } + if (min_i != start_i) { + u_int32_t t = ctx->list[start_i]; + ctx->list[start_i] = ctx->list[min_i]; + ctx->list[min_i] = t; + } + } +} + +char *day_1_part_1() { + struct list_pair *pair = parse_input(); + + selection_sort(pair->left); + selection_sort(pair->right); + + int total = 0; + for(int i = 0; i < 1000; i++) + total += pair->left->list[i] > pair->right->list[i] ? pair->left->list[i] - pair->right->list[i] : pair->right->list[i] - pair->left->list[i]; + + free(pair->left->list); + free(pair->right->list); + free(pair->left); + free(pair->right); + free(pair); + + int len = snprintf(NULL, 0, "%d", total); + char *total_str = malloc(len +1); + snprintf(total_str, len, "%d", total); + + return total_str; +} + +char *day_1_part_2() { + struct list_pair *pair = parse_input(); + int total = 0; + + // TODO: do not the O(n^2) + for(int left_i = 0; left_i < pair->left->list_len; left_i++) { + int c = 0; + for(int right_i = 0; right_i < pair->right->list_len; right_i++) + if(pair->left->list[left_i] == pair->right->list[right_i]) c++; + total += c*pair->left->list[left_i]; + } + + free(pair->left->list); + free(pair->right->list); + free(pair->left); + free(pair->right); + free(pair); + + int len = snprintf(NULL, 0, "%d", total) + 1; + char *total_str = malloc(len); + snprintf(total_str, len, "%d", total); + + return total_str; } \ No newline at end of file diff --git a/2024/src/include/day1.h b/2024/src/include/day1.h deleted file mode 100644 index e69de29..0000000 diff --git a/2024/src/include/days.h b/2024/src/include/days.h index 0c0992b..789e944 100644 --- a/2024/src/include/days.h +++ b/2024/src/include/days.h @@ -1,3 +1,3 @@ -#include "day1.h" +#include "day1_input.h" char *day_1_part_1(void); \ No newline at end of file diff --git a/adventofcode/src/main.c b/adventofcode/src/main.c index 0a555c1..021a7f8 100644 --- a/adventofcode/src/main.c +++ b/adventofcode/src/main.c @@ -43,9 +43,11 @@ int main(int argc, char** argv) { char *(*part_func)(void) = dlsym(handle, fname); if(part_func) { - fprintf(stderr, "day %s part %s: %s\n", argv[2], argv[3], part_func()); + char *r = part_func(); + fprintf(stderr, "day %s part %s: %s\n", argv[2], argv[3], r); + free(r); } else { - fprintf(stderr, "Failed to call %s", fname); + fprintf(stderr, "Failed to call %s: implementation missing!\n", fname); err = 4; }