day 12 part two wrong, day 13 done
[advent21.git] / 06a.c
diff --git a/06a.c b/06a.c
index 92a1b8a..e2634d2 100644 (file)
--- a/06a.c
+++ b/06a.c
@@ -4,31 +4,20 @@
 
 int main()
 {
-       char buf[1000];
-       if (fgets(buf, 1000, stdin) == NULL)
-               return 1;
-       long fish0[9] = {0};
-       long fish1[9] = {0};
-       long *this = &fish0[0];
-       long *that = &fish1[0];
+       char *buf = NULL;
+       size_t len = 0;
+       long fish[9] = {0};
 
+       getline(&buf, &len, stdin);
        char *p = strtok(buf, ",");
-       this[atoi(p)]++;
+       fish[atoi(p)]++;
        while ((p = strtok(NULL, ",")) != NULL)
-               this[atoi(p)]++;
+               fish[atoi(p)]++;
 
-       for (long day = 0; day<80; day++) {
-               that[8] = this[0];
-               for (long i = 0; i<8; i++)
-                       that[i] = this[i+1];
-               that[6] += this[0];
-               long *tmp = this;
-               this = that;
-               that = tmp;
-       }
+       for (long day = 0; day<80; day++)
+               fish[(day+7) % 9] += fish[day%9];
 
-       long sum = 0;
-       for (long i = 0; i<=8; i++)
-               sum += this[i];
-       printf("%lu\n", sum);
+       for (long i = 1; i<=8; i++)
+               fish[0] += fish[i];
+       printf("%ld\n", fish[0]);
 }