summaryrefslogtreecommitdiff
path: root/021.c
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 /021.c
parent04e79fa276ae1c3620868d85941b2c7b7c11222a (diff)
improve input handling & merge part 1 and 2 for the first days
Diffstat (limited to '021.c')
-rw-r--r--021.c67
1 files changed, 0 insertions, 67 deletions
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;
-}