From d8c0ff33b45a990d263d8c99f485fb0301f17cf9 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Thu, 9 Dec 2021 22:38:46 +0100 Subject: [PATCH] Add day09 2021 Part1 --- 2021/day09.c | 113 ++++++++++++++++++++++++++++++++++++++++++ 2021/inputs/day09.txt | 100 +++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 2021/day09.c create mode 100644 2021/inputs/day09.txt diff --git a/2021/day09.c b/2021/day09.c new file mode 100644 index 0000000..6e7bce2 --- /dev/null +++ b/2021/day09.c @@ -0,0 +1,113 @@ +#include +#include +#include +#include + +typedef struct list list; + +struct list { + list* next; + int elem; +}; + +list* add(list* l, int e) { + list* l_new = (list*)malloc(sizeof(int)+sizeof(list*)); + l_new->next = l; + l_new->elem = e; + return l_new; +}; + +bool has(list *l, int e) { + while (l != NULL) { + if (l->elem==e) { + return true; + }; + l=l->next; + }; + return false; +}; + +int size(list* l) { + int n = 0; + while (l!= NULL) { + n++; + l = l->next; + }; + return n; +}; + +void print_list(list* l) { + while (l != NULL) { + printf("%d->", l->elem); + l = l->next; + }; + printf("NULL\n"); +}; + +int value_of(list* l) { + int somme = 0; + while (l!=NULL) { + somme += 1 + l->elem; + l = l->next; + }; + return somme; +}; + +int* read_sample() { + FILE *fp; + char ch; + int length = 0; + fp = fopen( "inputs/day09.txt", "r"); + if (fp == NULL) { + perror("Error while opening the file.\n"); + exit(EXIT_FAILURE); + }; + while(fgetc(fp) != EOF) { + length++; + }; + + int* tab = (int*)malloc(sizeof(int)*length); + + fclose(fp); + + fp= fopen("inputs/day09.txt", "r"); + if (fp==NULL){ + perror("Error while opening the file.\n"); + exit(EXIT_FAILURE); + }; + int i=0; + while((ch=fgetc(fp))!=EOF) { + if (ch != '\n') { + tab[i] = ch - '0'; + i++; + }; + }; + return tab; +}; + +int part1(int* sample, int n, int p) { + list* l = NULL; + bool cur; + for (int i=0; i < n; i++) { + for (int j=0; j < p; j++) { + cur = true; + cur = cur && (j == 0||sample[i*p+j]