diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | 011.c | 32 | ||||
-rw-r--r-- | 012.c | 39 | ||||
-rw-r--r-- | 021.c | 67 | ||||
-rw-r--r-- | 022.c | 69 | ||||
-rw-r--r-- | Makefile | 24 | ||||
-rw-r--r-- | input.c | 55 | ||||
-rw-r--r-- | input.h | 7 |
8 files changed, 297 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54be510 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +??? +*.o +input/ +*.swp @@ -0,0 +1,32 @@ +#include <stdlib.h> +#include <stdio.h> +#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; +} @@ -0,0 +1,39 @@ +#include <stdlib.h> +#include <stdio.h> +#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; +} @@ -0,0 +1,67 @@ +#include <stdlib.h> +#include <stdio.h> +#include <err.h> +#include <string.h> +#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; +} @@ -0,0 +1,69 @@ +#include <stdlib.h> +#include <stdio.h> +#include <err.h> +#include <string.h> +#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) @@ -0,0 +1,55 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <err.h> + +/*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); +} @@ -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); |