From e7427596265f9e9b2bf5d0244284c5541ae5403d Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 1 Dec 2022 21:02:00 +0100 Subject: [PATCH] Finished day 1 in C! --- 2021/1/.gitignore | 3 +++ 2021/1/Makefile | 11 ++++++++++ 2021/1/depth.c | 28 ++++++++++++++++++++++++ 2021/1/depth2.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 +++ 5 files changed, 100 insertions(+) create mode 100644 2021/1/.gitignore create mode 100644 2021/1/Makefile create mode 100644 2021/1/depth.c create mode 100644 2021/1/depth2.c diff --git a/2021/1/.gitignore b/2021/1/.gitignore new file mode 100644 index 0000000..4b4b198 --- /dev/null +++ b/2021/1/.gitignore @@ -0,0 +1,3 @@ +depth +depth2 +input diff --git a/2021/1/Makefile b/2021/1/Makefile new file mode 100644 index 0000000..1f0ae9d --- /dev/null +++ b/2021/1/Makefile @@ -0,0 +1,11 @@ +.PHONY := all yeet + +all: build run + +build: + gcc -o depth depth.c + gcc -o depth2 depth2.c + +run: + ./depth + ./depth2 diff --git a/2021/1/depth.c b/2021/1/depth.c new file mode 100644 index 0000000..3162f74 --- /dev/null +++ b/2021/1/depth.c @@ -0,0 +1,28 @@ +#include +#include +#include + +int main() { + + FILE *input = fopen("input", "r"); + + if(input == NULL) { + printf("oh noes couldn't open the input file lol\n"); + return 1; + } + + + char line[255]; + int count = 0, last_num = INT_MAX; + while(fgets(line, sizeof(line), input) != NULL) { + int cur_num = atoi(line); + + count += cur_num > last_num; + last_num = cur_num; + } + + fclose(input); + + printf("There were %d measurements larger than the last.\n", count); + +} diff --git a/2021/1/depth2.c b/2021/1/depth2.c new file mode 100644 index 0000000..5e5b926 --- /dev/null +++ b/2021/1/depth2.c @@ -0,0 +1,55 @@ +#include +#include +#include + +#define WINDOW_SIZE 3 + +int main() { + + FILE *input = fopen("input", "r"); + + if(input == NULL) { + printf("oh noes couldn't open the input file lol\n"); + return 1; + } + + + char line[255]; + int index = 0, count = 0, last_num = INT_MAX; + int windows[WINDOW_SIZE][WINDOW_SIZE]; + + while(fgets(line, sizeof(line), input) != NULL) { + int cur_num = atoi(line), + cur_index = index % (WINDOW_SIZE); + + // Put values diagonal into the 3x3 grid, like this + // A B C + // B C A + // C A B + for(int i=0; i last_num; + last_num = sum; + } + + fclose(input); + + printf("There were %d measurements larger than the last.\n", count); + +} diff --git a/README.md b/README.md index c7bcc36..30daa67 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ Heya! This is my advent of code repo (as you might have guessed). In here you'll find my solutions of the advent of code exercises. +# 2021 + +So similar to last year, a different language each day. Different though: I pick the language myself instead of using the little seeded ransomizer. Hopefully this helps with me finishing it when I am busy that day (by picking a language I already know). ## 2020