refactor: executable per day
This commit is contained in:
@@ -3,8 +3,4 @@ project(advent_of_code_2023 C)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
add_executable(advent_of_code_2023 main.c
|
||||
main.h
|
||||
day_1.c
|
||||
array_list.c
|
||||
array_list.h)
|
||||
add_executable(day_01 day_1.c)
|
||||
|
||||
29
array_list.c
29
array_list.c
@@ -1,29 +0,0 @@
|
||||
//
|
||||
// Created by Lennart on 02/12/2023.
|
||||
//
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "array_list.h"
|
||||
|
||||
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_free(array_list *alist) {
|
||||
free(alist->data);
|
||||
free(alist);
|
||||
}
|
||||
27
array_list.h
27
array_list.h
@@ -23,3 +23,30 @@ 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_free(array_list *alist) {
|
||||
free(alist->data);
|
||||
free(alist);
|
||||
}
|
||||
|
||||
#endif //ARRAY_LIST_IMPLEMENTATION
|
||||
|
||||
6
day_1.c
6
day_1.c
@@ -3,11 +3,13 @@
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include "main.h"
|
||||
|
||||
#define ARRAY_LIST_IMPLEMENTATION
|
||||
#include "array_list.h"
|
||||
|
||||
int read_line(FILE *f);
|
||||
|
||||
int day_1() {
|
||||
int main() {
|
||||
FILE *f = fopen("day_1.txt", "r");
|
||||
if(f == NULL) {
|
||||
printf("Error! Missing input file!");
|
||||
|
||||
20
main.c
20
main.c
@@ -1,20 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "main.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if(argc <= 1) {
|
||||
printf("Please launch with a day number argument!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int day = (int) strtol(argv[1], (char**) NULL, 10);
|
||||
switch (day) {
|
||||
case 1:
|
||||
return day_1();
|
||||
default:
|
||||
printf("Unknown day: %i", day);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user