Add day06 2021
This commit is contained in:
parent
96d00882ed
commit
b61c6d1ce5
115
2021/day06.c
Normal file
115
2021/day06.c
Normal file
@ -0,0 +1,115 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
};
|
38
2021/day06.py
Executable file
38
2021/day06.py
Executable file
@ -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)}")
|
1
2021/inputs/day06.txt
Normal file
1
2021/inputs/day06.txt
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user