summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Douillet <vincent@vdouillet.fr>2021-12-03 17:01:37 +0100
committerVincent Douillet <vincent@vdouillet.fr>2021-12-03 17:01:37 +0100
commit736823f313bd2e00e49a1b52aaf0ea68a79db438 (patch)
tree5cf54f57e5c2afd8ea22baacb3ffa05a76a59fcc
parent04e79fa276ae1c3620868d85941b2c7b7c11222a (diff)
improve input handling & merge part 1 and 2 for the first days
-rw-r--r--01.c43
-rw-r--r--011.c32
-rw-r--r--012.c39
-rw-r--r--02.c (renamed from 022.c)51
-rw-r--r--021.c67
-rw-r--r--03.c33
-rw-r--r--Makefile12
-rw-r--r--input.c65
-rw-r--r--input.h23
9 files changed, 156 insertions, 209 deletions
diff --git a/01.c b/01.c
new file mode 100644
index 0000000..33c52aa
--- /dev/null
+++ b/01.c
@@ -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;
+}
diff --git a/011.c b/011.c
deleted file mode 100644
index 4af029f..0000000
--- a/011.c
+++ /dev/null
@@ -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;
-}
diff --git a/012.c b/012.c
deleted file mode 100644
index ac5b057..0000000
--- a/012.c
+++ /dev/null
@@ -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;
-}
diff --git a/022.c b/02.c
index c22aca8..30af77f 100644
--- a/022.c
+++ b/02.c
@@ -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], &currentMove);
+ 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], &currentMove);
+ for(size_t i = 0; i < input->line_count; i++) {
+ parse_move(input->lines[i], &currentMove);
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;
}
diff --git a/021.c b/021.c
deleted file mode 100644
index 8f0cd09..0000000
--- a/021.c
+++ /dev/null
@@ -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], &currentMove);
- 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/03.c b/03.c
index 02af910..d783b79 100644
--- a/03.c
+++ b/03.c
@@ -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;
}
diff --git a/Makefile b/Makefile
index baca8e6..61cd769 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/input.c b/input.c
index 5d184bb..1c72194 100644
--- a/input.c
+++ b/input.c
@@ -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", &current) != 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", &current) != 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);
}
diff --git a/input.h b/input.h
index 9e6762e..6507053 100644
--- a/input.h
+++ b/input.h
@@ -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