add day 1

This commit is contained in:
theBreadCompany 2024-12-02 02:00:37 +01:00
parent 3246535ae6
commit 9f97e0a390
6 changed files with 122 additions and 6 deletions

2
.vscode/launch.json vendored
View file

@ -6,7 +6,7 @@
"request": "launch", "request": "launch",
"name": "Launch", "name": "Launch",
"program": "${workspaceFolder}/build/adventofcode/adventofcode", "program": "${workspaceFolder}/build/adventofcode/adventofcode",
"args": ["2024", "1", "1"], "args": ["2024", "1", "2"],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"preLaunchTask": "CMake: build", "preLaunchTask": "CMake: build",
} }

View file

@ -13,6 +13,7 @@ endforeach()
add_custom_target(prebuild_target DEPENDS ${adv_input}) add_custom_target(prebuild_target DEPENDS ${adv_input})
add_dependencies(2024 prebuild_target) add_dependencies(2024 prebuild_target)
target_link_libraries(2024 m)
target_sources(2024 target_sources(2024
PRIVATE PRIVATE
src/day1.c src/day1.c

View file

@ -1,3 +1,116 @@
void *day_1_part_1() { #include "include/days.h"
return "yes"; #include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
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;
} }

View file

@ -1,3 +1,3 @@
#include "day1.h" #include "day1_input.h"
char *day_1_part_1(void); char *day_1_part_1(void);

View file

@ -43,9 +43,11 @@ int main(int argc, char** argv) {
char *(*part_func)(void) = dlsym(handle, fname); char *(*part_func)(void) = dlsym(handle, fname);
if(part_func) { 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 { } else {
fprintf(stderr, "Failed to call %s", fname); fprintf(stderr, "Failed to call %s: implementation missing!\n", fname);
err = 4; err = 4;
} }