From b61c6d1ce5310db82e0be711e8f842e3ffc1f161 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Tue, 7 Dec 2021 22:37:55 +0100 Subject: [PATCH] Add day06 2021 --- 2021/day06.c | 115 ++++++++++++++++++++++++++++++++++++++++++ 2021/day06.py | 38 ++++++++++++++ 2021/inputs/day06.txt | 1 + 3 files changed, 154 insertions(+) create mode 100644 2021/day06.c create mode 100755 2021/day06.py create mode 100644 2021/inputs/day06.txt diff --git a/2021/day06.c b/2021/day06.c new file mode 100644 index 0000000..1ca6cb4 --- /dev/null +++ b/2021/day06.c @@ -0,0 +1,115 @@ +#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"); +}; + +list* read_sample() { + FILE *fp; + char ch; + list* l = NULL; + fp = fopen( "inputs/day06.txt", "r"); + if (fp == NULL) { + perror("Error while opening the file.\n"); + exit(EXIT_FAILURE); + }; + + while((ch = fgetc(fp)) != EOF) { + if (ch != ',' && ch != '\n' ) { + l = add(l, ch - '0'); + }; + }; + fclose(fp); + return l; +}; + +int part1(list* sample, int days) { + list* start = sample; + for (int i=-1; i < days+1; i++) { + while(sample!=NULL) { + if (sample->elem==i-1) { + sample->elem = i+6; + start = add(start, i+8); + }; + sample = sample->next; + }; + sample = start; + printf("After %d days: %d\n", i, size(sample)); + }; + return size(start); +}; + +/*int part2(list* sample, int days) { + int tab[8]; + int tmp; + int somme = 0; + + for (int i=0; i < 8; i++) { + tab[i] = 0; + }; + while (sample!=NULL) { + tab[sample->elem]++; + sample = sample->next; + }; + for (int i=0; i < days+1; i++) { + tmp = tab[i%8]; + tab[(i+6)%8] += tmp; + printf("%d\n", tmp); + }; + + for (int i=0; i < 8; i++) { + somme += tab[i]; + } + return somme; +};*/ + +int main() { + list* l1 = read_sample(); + //list* l2 = read_sample(); + printf("Partie 1: %d\n", part1(l1, 80)); + //printf("Partie 2: %d\n", part2(l2, 256)); + // La partie II menant à des nombres trop grands, + // la gestion d'overflow devenant un problème, elle + // a été réalisée en Python + return 0; +}; diff --git a/2021/day06.py b/2021/day06.py new file mode 100755 index 0000000..60d4e44 --- /dev/null +++ b/2021/day06.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 +""" +Jour 06 du défi Advent Of Code pour l'année 2021 +""" + +def read_sample(): + """récupère les entrées depuis le fichier texte correspondant""" + with open('inputs/day06.txt', 'r') as f: + sample = f.read().split(',') + sample = [ int(i) for i in sample if i != '' ] + return sample + +def part1(sample): + """Partie 1 du défi""" + return NotImplementedError + +def part2(sample): + """Partie 2 du défi""" + print(sample) + fishes = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0} + for i in sample: + fishes[i] += 1 + for i in range(256): + (fishes[0], fishes[1], fishes[2], fishes[3], + fishes[4], fishes[5], fishes[6] ,fishes[7], fishes[8]) = ( + fishes[1], fishes[2], fishes[3], fishes[4], fishes[5], + fishes[6], fishes[7] + fishes[0], fishes[8], fishes[0]) + somme = 0 + for i in range(9): + somme += fishes[i] + return somme + + +def main(): + """Fonction principale""" + sample = read_sample() + print(f"part1: {part1(sample)}") + print(f"part2: {part2(sample)}") diff --git a/2021/inputs/day06.txt b/2021/inputs/day06.txt new file mode 100644 index 0000000..87ca79a --- /dev/null +++ b/2021/inputs/day06.txt @@ -0,0 +1 @@ +1,3,1,5,5,1,1,1,5,1,1,1,3,1,1,4,3,1,1,2,2,4,2,1,3,3,2,4,4,4,1,3,1,1,4,3,1,5,5,1,1,3,4,2,1,5,3,4,5,5,2,5,5,1,5,5,2,1,5,1,1,2,1,1,1,4,4,1,3,3,1,5,4,4,3,4,3,3,1,1,3,4,1,5,5,2,5,2,2,4,1,2,5,2,1,2,5,4,1,1,1,1,1,4,1,1,3,1,5,2,5,1,3,1,5,3,3,2,2,1,5,1,1,1,2,1,1,2,1,1,2,1,5,3,5,2,5,2,2,2,1,1,1,5,5,2,2,1,1,3,4,1,1,3,1,3,5,1,4,1,4,1,3,1,4,1,1,1,1,2,1,4,5,4,5,5,2,1,3,1,4,2,5,1,1,3,5,2,1,2,2,5,1,2,2,4,5,2,1,1,1,1,2,2,3,1,5,5,5,3,2,4,2,4,1,5,3,1,4,4,2,4,2,2,4,4,4,4,1,3,4,3,2,1,3,5,3,1,5,5,4,1,5,1,2,4,2,5,4,1,3,3,1,4,1,3,3,3,1,3,1,1,1,1,4,1,2,3,1,3,3,5,2,3,1,1,1,5,5,4,1,2,3,1,3,1,1,4,1,3,2,2,1,1,1,3,4,3,1,3