chore: some cleanup

This commit is contained in:
2023-12-02 18:15:41 +01:00
parent 0ee32e3cf6
commit 9ca52c19e7
4 changed files with 66 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.26)
cmake_minimum_required(VERSION 3.20)
project(advent_of_code_2023 C)
set(CMAKE_C_STANDARD 11)

View File

@@ -44,6 +44,15 @@ void alist_add(array_list *alist, ELEMENT_TYPE entry) {
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);

80
day_1.c
View File

@@ -3,51 +3,81 @@
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define ARRAY_LIST_IMPLEMENTATION
#include "array_list.h"
int read_line(FILE *f, bool replace_letters);
int solve(const char *fileName, bool replace_letters);
int read_line(FILE *f);
char *buffer;
int bufferLen = 64;
const char* digitsAsLetters[] = {
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};
int main() {
FILE *f = fopen("day_1.txt", "r");
buffer = malloc(bufferLen * sizeof (char));
printf("Result #1: %i\n", solve("day_1.txt", false));
printf("Result #2: %i\n", solve("day_1.txt", true));
free(buffer);
return 0;
}
int solve(const char *fileName, bool replace_letters) {
FILE *f = fopen(fileName, "r");
if(f == NULL) {
printf("Error! Missing input file!");
return -1;
exit(-1);
}
int sum = 0;
while(!feof(f)) {
sum += read_line(f);
sum += read_line(f, replace_letters);
}
fclose(f);
printf("Result: %i\n", sum);
return 0;
return sum;
}
// Read all digits form the next line in a file
int read_line(FILE *f) {
int read_line(FILE *f, bool replace_letters) {
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;
// Read the entire line as a string
int position = 0;
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;
if(position >= bufferLen - 1) {
buffer = realloc(buffer, 2 * sizeof(char) * bufferLen);
bufferLen *= 2;
}
fgets(&buffer[position], bufferLen - position, f);
position = bufferLen - 1;
} while(buffer[strlen(buffer) - 1] != '\n' && !feof(f));
for(int i = 0; i < bufferLen; i++) {
if(buffer[i] == 0) {
break;
}
int digit = -1;
if(sscanf(&buffer[i], "%1d", &digit) != 1 && replace_letters) {
for(int j = 1; j < 10; j++) {
if(strncmp(&buffer[i], digitsAsLetters[j], strlen(digitsAsLetters[j])) == 0) {
digit = j;
break;
}
last = digit;
}
}
} while(!feof(f));
result:
if(digit >= 0) {
if(first == -1) {
first = digit;
}
last = digit;
}
}
return first * 10 + last;
}

View File

@@ -13,7 +13,7 @@ fourtwoxsxqqmqf3sixfoursixmmjhdlx
bcbsfd14cjg
95three6threendpqpjmbpcblone
tdmvthreeonefive8574
5eight82sixtwonev
58826twonev
ninemg2shhmsqh
thmlz4
xtxjmm2tbbntrmdqxpkdjgh1vzjvtvg7nine