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