diff --git a/2024/Day2/.clang-format b/2024/Day2/.clang-format new file mode 100644 index 0000000..3773a50 --- /dev/null +++ b/2024/Day2/.clang-format @@ -0,0 +1,6 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +ColumnLimit: 100 +PointerAlignment: Right +AlignAfterOpenBracket: BlockIndent +AlignArrayOfStructures: Left diff --git a/2024/Day2/Makefile b/2024/Day2/Makefile new file mode 100644 index 0000000..625f61e --- /dev/null +++ b/2024/Day2/Makefile @@ -0,0 +1,16 @@ +CC = gcc +CC_FLAGS = -Wall -Wextra -Werror -Wpedantic -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function +CC_LINK_FLAGS = + +.PHONY: all run clean +all: run + +run: build + ./target/main.o + +build: src/main.c + $(CC) -o target/main.o src/main.c $(CC_FLAGS) $(CC_LINK_FLAGS) + chmod u+x target/main.o + +clean: + rm -r target/* diff --git a/2024/Day2/src/main.c b/2024/Day2/src/main.c index e69de29..cb88125 100644 --- a/2024/Day2/src/main.c +++ b/2024/Day2/src/main.c @@ -0,0 +1,70 @@ +#include +#include +#include + +#define BUFFER_SIZE 256 + +int count_lines(char *filename); + +int main() { + FILE *file; + char filename[] = "input.txt"; + char *buffer = malloc(BUFFER_SIZE); + char *saveptr; + char *current_number_string; + int count = 0; + + file = fopen(filename, "r"); + + if (file == NULL) { + perror("Error opening file"); + return 1; + } + + while (fgets(buffer, BUFFER_SIZE, file) != NULL) { + char *line = buffer; + int previous_level = -1; + int is_decreasing = -1; + int is_last = 0; + while ((current_number_string = strtok_r(line, " ", &saveptr))) { + if (line) { + line = NULL; + } + if (current_number_string[strlen(current_number_string) - 1] == '\n') { + current_number_string[strlen(current_number_string) - 1] = '\0'; + is_last = 1; + } + int level = atoi(current_number_string); + if (previous_level == -1) { + previous_level = level; + continue; + } + + if (abs(level - previous_level) < 1 || abs(level - previous_level) > 3) { + break; + } + + if (is_decreasing == -1) { + is_decreasing = (level < previous_level) ? 1 : 0; + } + if (is_decreasing && level < previous_level) { + previous_level = level; + if (is_last) { + count++; + } + } else if (!is_decreasing && level > previous_level) { + previous_level = level; + if (is_last) { + count++; + } + } else { + break; + } + } + } + printf("%d", count); + + fclose(file); + + return 0; +} diff --git a/2024/Day9/Makefile b/2024/Day9/Makefile new file mode 100644 index 0000000..625f61e --- /dev/null +++ b/2024/Day9/Makefile @@ -0,0 +1,16 @@ +CC = gcc +CC_FLAGS = -Wall -Wextra -Werror -Wpedantic -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function +CC_LINK_FLAGS = + +.PHONY: all run clean +all: run + +run: build + ./target/main.o + +build: src/main.c + $(CC) -o target/main.o src/main.c $(CC_FLAGS) $(CC_LINK_FLAGS) + chmod u+x target/main.o + +clean: + rm -r target/*