chore: initial commit
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
cmake-build-*
|
||||||
|
build/
|
||||||
|
.idea/
|
||||||
10
CMakeLists.txt
Normal file
10
CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.26)
|
||||||
|
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)
|
||||||
29
array_list.c
Normal file
29
array_list.c
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
//
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
25
array_list.h
Normal file
25
array_list.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
//
|
||||||
|
// 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
|
||||||
51
day_1.c
Normal file
51
day_1.c
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
//
|
||||||
|
// Created by Lennart on 02/12/2023.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
int read_line(FILE *f);
|
||||||
|
|
||||||
|
int day_1() {
|
||||||
|
FILE *f = fopen("day_1.txt", "r");
|
||||||
|
if(f == NULL) {
|
||||||
|
printf("Error! Missing input file!");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
while(!feof(f)) {
|
||||||
|
sum += read_line(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
printf("Result: %i\n", sum);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read all digits form the next line in a file
|
||||||
|
int read_line(FILE *f) {
|
||||||
|
int first = -1, last = 10; // Use first=-1 to detect unset value and last=10 to return 0 (-10 + 10) when no values are set
|
||||||
|
char buffer[16];
|
||||||
|
int digit;
|
||||||
|
do {
|
||||||
|
fgets(&buffer[0], 16, f);
|
||||||
|
for(int i = 0; i < 16; i++) {
|
||||||
|
if(buffer[i] == '\n') {
|
||||||
|
goto result;
|
||||||
|
} else if(buffer[i] == 0) {
|
||||||
|
break; // End of buffer
|
||||||
|
}
|
||||||
|
if(sscanf(&buffer[i], "%1d", &digit) == 1) { // NOLINT(*-err34-c)
|
||||||
|
if(first == -1) {
|
||||||
|
first = digit;
|
||||||
|
}
|
||||||
|
last = digit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while(!feof(f));
|
||||||
|
|
||||||
|
result:
|
||||||
|
return first * 10 + last;
|
||||||
|
}
|
||||||
20
main.c
Normal file
20
main.c
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#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