From c851f16d1df21d7579af72d842521b951261b092 Mon Sep 17 00:00:00 2001 From: Vincent Douillet Date: Thu, 2 Dec 2021 22:46:01 +0100 Subject: day 1 & 2 --- .gitignore | 4 ++++ 011.c | 32 +++++++++++++++++++++++++++++ 012.c | 39 +++++++++++++++++++++++++++++++++++ 021.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 022.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 24 ++++++++++++++++++++++ input.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ input.h | 7 +++++++ 8 files changed, 297 insertions(+) create mode 100644 .gitignore create mode 100644 011.c create mode 100644 012.c create mode 100644 021.c create mode 100644 022.c create mode 100644 Makefile create mode 100644 input.c create mode 100644 input.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54be510 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +??? +*.o +input/ +*.swp diff --git a/011.c b/011.c new file mode 100644 index 0000000..4af029f --- /dev/null +++ b/011.c @@ -0,0 +1,32 @@ +#include +#include +#include "input.h" + +#define INPUT "input/01.txt" + +int main() { + // lecture du fichier d'entree + FILE* file=fopen(INPUT,"r"); + if(file == NULL) { + printf("Le fichier %s n'existe pas\n", INPUT); + return -1; + } + size_t lineCount = count_lines(file); + int* input = malloc(lineCount * sizeof(int)); + if(read_lines_as_int(file, input, lineCount) != 0) { + printf("Erreur de parsing du fichier\n"); + return -1; + } + // fermeture du fichier + fclose(file); + + // calcul des variations de profondeur + int result = 0; + for(size_t i = 1; i < lineCount; i++) { + if(input[i] > input[i-1]) + result++; + } + printf("%d\n", result); + + return 0; +} diff --git a/012.c b/012.c new file mode 100644 index 0000000..ac5b057 --- /dev/null +++ b/012.c @@ -0,0 +1,39 @@ +#include +#include +#include "input.h" + +#define INPUT "input/01.txt" + +int main() { + // lecture du fichier d'entree + FILE* file=fopen(INPUT,"r"); + if(file == NULL) { + printf("Le fichier %s n'existe pas\n", INPUT); + return -1; + } + size_t lineCount = count_lines(file); + int* input = malloc(lineCount * sizeof(int)); + if(read_lines_as_int(file, input, lineCount) != 0) { + printf("Erreur de parsing du fichier\n"); + return -1; + } + // fermeture du fichier + fclose(file); + + // calcul des variations de profondeur + int result = 0; + int windowSum = input[0] + input[1] + input[2]; + for(size_t i = 1; i < lineCount - 2; i++) { + int newWindowSum = input[i] + input[i+1] + input[i+2]; + if(newWindowSum > windowSum) + result++; + + windowSum = newWindowSum; + } + printf("%d\n", result); + + // nettoyage + free(input); + + return 0; +} diff --git a/021.c b/021.c new file mode 100644 index 0000000..8f0cd09 --- /dev/null +++ b/021.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include "input.h" + +#define INPUT "input/02.txt" + +enum direction { forward, up, down }; + +struct submarine_move { + enum direction dir; + int length; +}; + +void parse_move(char* string, struct submarine_move* move) { + if(*string == 'f') + move->dir = forward; + else if(*string == 'u') + move->dir = up; + else if(*string == 'd') + move->dir = down; + else + err(1, "direction inconnue %c", *string); + + size_t stringLength = strlen(string); + char digit = string[stringLength - 2]; + if(digit < '0' || digit > '9') + err(1, "longueur inconnue %c", digit); + move->length = digit - '0'; +} + +int main() { + // lecture du fichier d'entree + FILE* file=fopen(INPUT,"r"); + if(file == NULL) + err(1, "Le fichier %s n'existe pas\n", INPUT); + + size_t lineCount = count_lines(file); + char** input = malloc(lineCount * sizeof(char*)); + read_lines_as_string(file, input, lineCount); + // fermeture du fichier + fclose(file); + + // l'algo + struct submarine_move currentMove; + long forwardDst = 0; + long downDst = 0; + for(size_t i = 0; i < lineCount; i++) { + parse_move(input[i], ¤tMove); + switch(currentMove.dir) { + case forward: + forwardDst += currentMove.length; + continue; + case up: + downDst -= currentMove.length; + continue; + case down: + downDst += currentMove.length; + continue; + } + } + printf("%ld\n", forwardDst * downDst); + + free_strings(input, lineCount); + return 0; +} diff --git a/022.c b/022.c new file mode 100644 index 0000000..c22aca8 --- /dev/null +++ b/022.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include "input.h" + +#define INPUT "input/02.txt" + +enum direction { forward, up, down }; + +struct submarine_move { + enum direction dir; + int length; +}; + +void parse_move(char* string, struct submarine_move* move) { + if(*string == 'f') + move->dir = forward; + else if(*string == 'u') + move->dir = up; + else if(*string == 'd') + move->dir = down; + else + err(1, "direction inconnue %c", *string); + + size_t stringLength = strlen(string); + char digit = string[stringLength - 2]; + if(digit < '0' || digit > '9') + err(1, "longueur inconnue %c", digit); + move->length = digit - '0'; +} + +int main() { + // lecture du fichier d'entree + FILE* file=fopen(INPUT,"r"); + if(file == NULL) + err(1, "Le fichier %s n'existe pas\n", INPUT); + + size_t lineCount = count_lines(file); + char** input = malloc(lineCount * sizeof(char*)); + read_lines_as_string(file, input, lineCount); + // fermeture du fichier + fclose(file); + + // l'algo + struct submarine_move currentMove; + long forwardDst = 0; + long downDst = 0; + long aim = 0; + for(size_t i = 0; i < lineCount; i++) { + parse_move(input[i], ¤tMove); + switch(currentMove.dir) { + case forward: + forwardDst += currentMove.length; + downDst += currentMove.length * aim; + continue; + case up: + aim -= currentMove.length; + continue; + case down: + aim += currentMove.length; + continue; + } + } + printf("%ld\n", forwardDst * downDst); + + free_strings(input, lineCount); + return 0; +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1a2b7e9 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +CC=cc +CFLAGS=-std=c1x -W -Wall -g +LDFLAGS= +EXEC=011 012 021 022 + +all: $(EXEC) + +011: input.o 011.o + $(CC) -o $@ $> $(LDFLAGS) + +012: input.o 012.o + $(CC) -o $@ $> $(LDFLAGS) + +021: input.o 021.o + $(CC) -o $@ $> $(LDFLAGS) + +022: input.o 022.o + $(CC) -o $@ $> $(LDFLAGS) + +.c.o: + $(CC) -o $@ -c $< $(CFLAGS) + +clean: + rm -rf *.o $(EXEC) diff --git a/input.c b/input.c new file mode 100644 index 0000000..5d184bb --- /dev/null +++ b/input.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +/*public*/size_t count_lines(FILE* file) { + size_t lineCount = 0; + char currentChar; + // on part du debut + // TODO verifier le code de retour + fseek(file, 0, SEEK_SET); + while((currentChar=fgetc(file)) != EOF) { + if(currentChar == '\n') + lineCount++; + } + return lineCount; +} + +/*public*/int read_lines_as_int(FILE* file, int* result, int resultSize) { + // retour au debut du fichier et lecture des lignes + // TODO verifier le code de retour + fseek(file, 0, SEEK_SET); + int current; + for(int lineIndex = 0; lineIndex < resultSize; lineIndex++) { + if(fscanf(file, "%d", ¤t) != 1) { + result[lineIndex] = 0; + printf("Erreur de lecture ligne %d\n", lineIndex); + continue; + } + result[lineIndex] = current; + } + + return 0; +} + +/*public*/int read_lines_as_string(FILE* file, char** result, int resultSize) { + // retour au debut du fichier et lecture des lignes + // TODO verifier le code de retour + fseek(file, 0, SEEK_SET); + size_t lineSize = 0; + for(int lineIndex = 0; lineIndex < resultSize; lineIndex++) { + char** dst = &result[lineIndex]; + if(getline(dst, &lineSize, file) < 0) + err(1, "Erreur de lecture ligne %d\n", lineIndex); + } + + return 0; +} + +/*public*/void free_strings(char** strings, size_t stringCount) { + for(size_t i = 0; i < stringCount; i++) { + free(strings[i]); + } + free(strings); +} diff --git a/input.h b/input.h new file mode 100644 index 0000000..9e6762e --- /dev/null +++ b/input.h @@ -0,0 +1,7 @@ +size_t count_lines(FILE* file); + +int read_lines_as_int(FILE* file, int* result, int resultSize); + +int read_lines_as_string(FILE* file, char** result, int resultSize); + +void free_strings(char** strings, size_t stringCount); -- cgit v1.2.3