diff options
-rw-r--r-- | 01.c | 43 | ||||
-rw-r--r-- | 011.c | 32 | ||||
-rw-r--r-- | 012.c | 39 | ||||
-rw-r--r-- | 02.c (renamed from 022.c) | 51 | ||||
-rw-r--r-- | 021.c | 67 | ||||
-rw-r--r-- | 03.c | 33 | ||||
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | input.c | 65 | ||||
-rw-r--r-- | input.h | 23 |
9 files changed, 156 insertions, 209 deletions
@@ -0,0 +1,43 @@ +#include <stdlib.h> +#include <stdio.h> +#include "input.h" + +#define INPUT "input/01.txt" + +void part1(struct input_int* input) { + // compute depth variations + int result = 0; + for(size_t i = 1; i < input->line_count; i++) { + if(input->lines[i] > input->lines[i-1]) + result++; + } + printf("%d\n", result); +} + +void part2(struct input_int* input) { + // compute depth variations + int result = 0; + int windowSum = input->lines[0] + input->lines[1] + input->lines[2]; + for(size_t i = 1; i < input->line_count - 2; i++) { + int newWindowSum = input->lines[i] + input->lines[i+1] + input->lines[i+2]; + if(newWindowSum > windowSum) + result++; + + windowSum = newWindowSum; + } + printf("%d\n", result); +} + +int main() { + // read input + struct input_int input; + input_int_read(&input, INPUT); + + // do stuff + part1(&input); + part2(&input); + + // cleanup & exit + input_int_free(&input); + return 0; +} @@ -1,32 +0,0 @@ -#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; -} @@ -1,39 +0,0 @@ -#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; -} @@ -1,4 +1,3 @@ -#include <stdlib.h> #include <stdio.h> #include <err.h> #include <string.h> @@ -30,25 +29,34 @@ void parse_move(char* string, struct submarine_move* move) { 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); +void part1(struct input_str* input) { + struct submarine_move currentMove; + long forwardDst = 0; + long downDst = 0; + for(size_t i = 0; i < input->line_count; i++) { + parse_move(input->lines[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); +} - // l'algo +void part2(struct input_str* input) { 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); + for(size_t i = 0; i < input->line_count; i++) { + parse_move(input->lines[i], ¤tMove); switch(currentMove.dir) { case forward: forwardDst += currentMove.length; @@ -63,7 +71,18 @@ int main() { } } printf("%ld\n", forwardDst * downDst); +} + +int main() { + // read input data + struct input_str input; + input_str_read(&input, INPUT); + + // do stuff + part1(&input); + part2(&input); - free_strings(input, lineCount); + // cleanup & exit + input_str_free(&input); return 0; } @@ -1,67 +0,0 @@ -#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; -} @@ -1,20 +1,20 @@ #include <stdlib.h> #include <stdio.h> #include <err.h> -#include <string.h> #include "input.h" #define INPUT "input/03.txt" #define INPUT_SIZE 12 -void part1(char** input, size_t lineCount) { +void part1(struct input_str* input) { int oneCount[INPUT_SIZE]; + // TODO is that necessary ? for(int k = 0; k < INPUT_SIZE; k++) { oneCount[k] = 0; } - for(size_t i = 0; i < lineCount; i++) { - char* line = input[i]; + for(size_t i = 0; i < input->line_count; i++) { + char* line = input->lines[i]; for(int j = 0; j < INPUT_SIZE; j++) { if(line[j] == '1') oneCount[j]++; @@ -24,7 +24,7 @@ void part1(char** input, size_t lineCount) { // null terminated string char binary[INPUT_SIZE + 1]; for(int h = 0; h < INPUT_SIZE; h++) { - binary[h] = oneCount[h] > lineCount / 2.0f ? '1' : '0'; + binary[h] = oneCount[h] > input->line_count / 2.0f ? '1' : '0'; } binary[INPUT_SIZE] = '\0'; char* endp = NULL; @@ -37,19 +37,14 @@ void part1(char** input, size_t lineCount) { } 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); - - part1(input, lineCount); - - free_strings(input, lineCount); + // read input data + struct input_str input; + input_str_read(&input, INPUT); + + // do stuff + part1(&input); + + // cleanup & exit + input_str_free(&input); return 0; } @@ -1,20 +1,14 @@ CC=cc CFLAGS=-std=c1x -W -Wall -g LDFLAGS= -EXEC=011 012 021 022 03 +EXEC=01 02 03 all: $(EXEC) -011: input.o 011.o +01: input.o 01.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 +02: input.o 02.o $(CC) -o $@ $> $(LDFLAGS) 03: input.o 03.o @@ -2,8 +2,9 @@ #include <stdio.h> #include <string.h> #include <err.h> +#include "input.h" -/*public*/size_t count_lines(FILE* file) { +size_t count_lines(FILE* file) { size_t lineCount = 0; char currentChar; // on part du debut @@ -16,40 +17,58 @@ 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 +void input_int_read(struct input_int* result, char* filename) { + // open input file + FILE* file=fopen(filename, "r"); + if(file == NULL) + err(1, "cannot open file %s\n", filename); + // compute line count + result->line_count = count_lines(file); + // read each line of the file + // TODO check return code 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; + result->lines = malloc(result->line_count * sizeof(int)); + for(size_t lineIndex = 0; lineIndex < result->line_count; lineIndex++) { + if(fscanf(file, "%d", ¤t) != 1) + err(1, "parsing error line %ld\n", lineIndex); + + result->lines[lineIndex] = current; } - return 0; + // close file + fclose(file); } -/*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 +void input_str_read(struct input_str* result, char* filename) { + // open input file + FILE* file=fopen(filename, "r"); + if(file == NULL) + err(1, "cannot open file %s\n", filename); + // compute line count + result->line_count = count_lines(file); + + // read each line of the file + // TODO check return code fseek(file, 0, SEEK_SET); size_t lineSize = 0; - for(int lineIndex = 0; lineIndex < resultSize; lineIndex++) { - char** dst = &result[lineIndex]; + result->lines = malloc(result->line_count * sizeof(char*)); + for(size_t lineIndex = 0; lineIndex < result->line_count; lineIndex++) { + char** dst = &(result->lines[lineIndex]); if(getline(dst, &lineSize, file) < 0) - err(1, "Erreur de lecture ligne %d\n", lineIndex); + err(1, "read error line %ld\n", lineIndex); } - return 0; + fclose(file); +} + +void input_int_free(struct input_int* input) { + free(input->lines); } -/*public*/void free_strings(char** strings, size_t stringCount) { - for(size_t i = 0; i < stringCount; i++) { - free(strings[i]); +void input_str_free(struct input_str* input) { + for(size_t i = 0; i < input->line_count; i++) { + free(input->lines[i]); } - free(strings); + free(input->lines); } @@ -1,7 +1,22 @@ -size_t count_lines(FILE* file); +#ifndef DEF_INPUTH +#define DEF_INPUTH -int read_lines_as_int(FILE* file, int* result, int resultSize); +struct input_int { + size_t line_count; + int* lines; +}; -int read_lines_as_string(FILE* file, char** result, int resultSize); +struct input_str { + size_t line_count; + char** lines; +}; -void free_strings(char** strings, size_t stringCount); +void input_int_read(struct input_int* result, char* filename); + +void input_str_read(struct input_str* result, char* filename); + +void input_int_free(struct input_int* input); + +void input_str_free(struct input_str* input); + +#endif |