init day 2

This commit is contained in:
theBreadCompany 2024-12-03 08:54:44 +01:00
parent 9f97e0a390
commit 0bb93ccb17
6 changed files with 1093 additions and 9 deletions

2
.vscode/launch.json vendored
View file

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

View file

@ -1,6 +1,6 @@
add_library(2024 SHARED)
foreach(i RANGE 1 1)
foreach(i RANGE 1 2)
set(out_path "${CMAKE_CURRENT_SOURCE_DIR}/src/include/day${i}_input.h")
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/include/day${i}_input.h"
@ -17,4 +17,5 @@ target_link_libraries(2024 m)
target_sources(2024
PRIVATE
src/day1.c
src/day2.c
)

1000
2024/input/day2.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,4 @@
#include "include/days.h"
#include "include/day1_input.h"
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
@ -12,7 +12,7 @@ struct list_pair {
struct list_ctx *right;
};
struct list_pair *parse_input() {
struct list_pair *parse_input_day1() {
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));
@ -31,6 +31,7 @@ struct list_pair *parse_input() {
if (list_buf_len) {
ctx->left->list[list_i] = strtoul(list_buf, NULL, 10);
list_buf_len = 0;
free(list_buf);
list_buf = NULL;
};
break;
@ -38,6 +39,7 @@ struct list_pair *parse_input() {
ctx->right->list[list_i] = strtoul(list_buf, NULL, 10);
list_i++;
list_buf_len = 0;
free(list_buf);
list_buf = NULL;
break;
}
@ -68,7 +70,7 @@ void selection_sort(struct list_ctx *ctx) {
}
char *day_1_part_1() {
struct list_pair *pair = parse_input();
struct list_pair *pair = parse_input_day1();
selection_sort(pair->left);
selection_sort(pair->right);
@ -91,7 +93,7 @@ char *day_1_part_1() {
}
char *day_1_part_2() {
struct list_pair *pair = parse_input();
struct list_pair *pair = parse_input_day1();
int total = 0;
// TODO: do not the O(n^2)

84
2024/src/day2.c Normal file
View file

@ -0,0 +1,84 @@
#include "include/day2_input.h"
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
struct list_ctx {
void** list;
size_t list_len;
};
struct list_ctx *parse_input_day2() {
struct list_ctx *ctx = malloc(sizeof(struct list_ctx));
ctx->list = malloc(1000 * sizeof(struct list_ctx));
ctx->list_len = 1000;
int list_i = 0;
int report_buf_len = 0;
int report_i = 0;
char *report_buf = NULL;
ctx->list[list_i] = malloc(sizeof(struct list_ctx));
ctx->list_len = 0;
for(int i = 0; i < input_day2_txt_len; i++) {
char c = input_day2_txt[i];
struct list_ctx *report = ((struct list_ctx*) ctx->list[list_i]);
switch(c) {
case ' ':
if (report_buf_len) {
report->list = realloc(report->list, sizeof(u_int8_t) * (report_i+1));
report->list[report_i] = malloc(sizeof(u_int8_t));
*(u_int8_t*) report->list[report_i] = strtoul(report_buf, NULL, 10);
report_buf_len = 0;
free(report_buf);
report_buf = NULL;
report_i++;
fprintf(stderr, "%lu", strtoul(report_buf, NULL, 10));
};
break;
case '\n': {
report->list = realloc(report->list, sizeof(u_int8_t) * (report_i+1));
report->list[report_i] = malloc(sizeof(u_int8_t));
*(u_int8_t*) report->list[report_i] = strtoul(report_buf, NULL, 10);
list_i++;
report_buf_len = 0;
free(report_buf);
report_buf = NULL;
fprintf(stderr, "%lu\n", strtoul(report_buf, NULL, 10));
report_i = 0;
break;
}
default: {
report_buf = realloc(report_buf, (report_buf_len++) * sizeof(char));
report_buf[report_buf_len-1] = c;
}
}
}
return ctx;
}
char* day_2_part_1() {
struct list_ctx *ctx = parse_input_day2();
int total = 0;
for(int list_i = 0; list_i < ctx->list_len; list_i++) {
struct list_ctx *report = ((struct list_ctx*) ctx->list[list_i]);
bool safe = true;
for(int report_i = 1; report_i < report->list_len && safe; report_i++) {
int diff = *(u_int8_t*) report->list[report_i] - *(u_int8_t *) report->list[report_i];
safe = diff < 3 && diff > -3;
}
total += safe;
}
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 +0,0 @@
#include "day1_input.h"
char *day_1_part_1(void);