refactor: make list more generic

This commit is contained in:
2023-12-03 12:51:53 +01:00
parent db2c43032f
commit b88ad0abf9
4 changed files with 77 additions and 71 deletions

View File

@@ -1,12 +1,9 @@
cmake_minimum_required(VERSION 3.20)
project(advent_of_code_2023 C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD 17)
add_executable(day_01 day_1.c
input_handler.c
input_handler.h)
add_executable(day_02 day_2.c
input_handler.c
input_handler.h)
add_executable(day_01 day_1.c input_handler.c)
add_executable(day_02 day_2.c input_handler.c)
target_link_libraries(day_02 m)

View File

@@ -1,61 +0,0 @@
//
// Created by Lennart on 02/12/2023.
//
#ifndef ADVENT_OF_CODE_2023_ARRAY_LIST_H
#ifndef ELEMENT_TYPE
#define ELEMENT_TYPE int
#endif
typedef struct array_list_struct {
ELEMENT_TYPE *data;
int capacity;
int length;
} array_list;
array_list* alist_create(int capacity);
void alist_add(array_list *alist, ELEMENT_TYPE entry);
void alist_free(array_list *alist);
#define ADVENT_OF_CODE_2023_ARRAY_LIST_H
#endif //ADVENT_OF_CODE_2023_ARRAY_LIST_H
//#define ARRAY_LIST_IMPLEMENTATION
#ifdef ARRAY_LIST_IMPLEMENTATION
array_list* alist_create(int capacity) {
array_list *alist = malloc(sizeof (struct array_list_struct));
alist->data = malloc(sizeof (ELEMENT_TYPE) * capacity);
alist->capacity = capacity;
alist->length = 0;
return alist;
}
void alist_add(array_list *alist, ELEMENT_TYPE entry) {
if(alist->length >= alist->capacity) {
alist->data = realloc(alist->data, alist->capacity * 2);
alist->capacity = alist->capacity * 2;
}
alist->data[alist->length++] = entry;
}
void alist_remove(array_list *alist, int index) {
#ifdef ARRAY_LIST_UNORDERED
alist->data[index] = alist->data[--alist->length];
#else
memcpy(&alist->data[index], &alist->data[index + 1], sizeof(ELEMENT_TYPE) * (alist->capacity - index - 1));
alist->length--;
#endif //ARRAY_LIST_UNORDERED
}
void alist_free(array_list *alist) {
free(alist->data);
free(alist);
}
#endif //ARRAY_LIST_IMPLEMENTATION

11
day_2.c
View File

@@ -2,6 +2,8 @@
// Created by Lennart on 02/12/2023.
//
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -18,7 +20,9 @@ int bufferLen;
#ifdef _WIN32
#define STRTOK_TS strtok_s
#else
#elif defined(__STDC_LIB_EXT1__)
#define STRTOK_TS strtok_s
#elif _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
#define STRTOK_TS strtok_r
#endif //_WIN32
@@ -39,12 +43,12 @@ void main() {
char *sets = &line[strlen("Game : ") + (int) floor(log10(abs(gameId))) + 1];
char *set_context;
const char *set = STRTOK_TS(sets, ";", &set_context);
char *set = STRTOK_TS(sets, ";", &set_context);
int maxRed = 0, maxGreen = 0, maxBlue = 0;
bool fits = true;
while(set != NULL) {
const char *cube = strtok(set, ",");
char *cube = strtok(set, ",");
int red = 0, green = 0, blue = 0;
while(cube != NULL) {
int amount;
@@ -67,6 +71,7 @@ void main() {
maxBlue = blue > maxBlue ? blue : maxBlue;
set = STRTOK_TS(NULL, ";", &set_context);
free(cube);
}
result1 += fits ? gameId : 0;

65
list.h Normal file
View File

@@ -0,0 +1,65 @@
//
// Created by Lennart on 02/12/2023.
//
#include <stdlib.h>
#include <string.h>
#ifndef LIST_NAME
#define LIST_NAME List
#endif //LIST_NAME
#ifndef LIST_PREFIX
#define LIST_PREFIX list
#endif //LIST_PREFIX
#ifndef ELEMENT_TYPE
#define ELEMENT_TYPE void*
#endif //ELEMENT_TYPE
#define LIST_CONCAT(x, y) x ## _ ## y
#define LIST_FN(name) LIST_CONCAT(LIST_PREFIX, name)
typedef struct {
ELEMENT_TYPE *data;
int capacity;
int length;
} LIST_NAME;
__inline LIST_NAME* LIST_FN(create)(int capacity) {
LIST_NAME *alist = malloc(sizeof (LIST_NAME));
alist->data = malloc(sizeof (ELEMENT_TYPE) * capacity);
alist->capacity = capacity;
alist->length = 0;
return alist;
}
__inline void LIST_FN(add)(LIST_NAME *alist, ELEMENT_TYPE value) {
if(alist->length >= alist->capacity) {
alist->data = realloc(alist->data, alist->capacity * 2);
alist->capacity = alist->capacity * 2;
}
alist->data[alist->length++] = value;
}
__inline void LIST_FN(remove)(LIST_NAME *alist, int index) {
#ifdef LIST_UNORDERED
alist->data[index] = alist->data[--alist->length];
#else
memmove(&alist->data[index], &alist->data[index + 1], sizeof(ELEMENT_TYPE) * (alist->capacity - index - 1));
alist->length--;
#endif //LIST_UNORDERED
}
__inline void LIST_FN(free)(LIST_NAME *alist) {
free(alist->data);
free(alist);
}
#undef LIST_NAME
#undef LIST_PREFIX
#undef ELEMENT_TYPE
#undef LIST_CONCAT
#undef LIST_FN
#undef LIST_UNORDERED