summaryrefslogtreecommitdiff
path: root/012.c
diff options
context:
space:
mode:
authorVincent Douillet <vincent@vdouillet.fr>2021-12-02 22:46:01 +0100
committerVincent Douillet <vincent@vdouillet.fr>2021-12-02 22:46:01 +0100
commitc851f16d1df21d7579af72d842521b951261b092 (patch)
tree2f1c4672b53afb8c575ff7c64d88b9020034219d /012.c
day 1 & 2
Diffstat (limited to '012.c')
-rw-r--r--012.c39
1 files changed, 39 insertions, 0 deletions
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 <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;
+}