speed up
authorMart Lubbers <mart@martlubbers.net>
Mon, 6 Dec 2021 08:34:56 +0000 (09:34 +0100)
committerMart Lubbers <mart@martlubbers.net>
Mon, 6 Dec 2021 08:34:56 +0000 (09:34 +0100)
06a.c
06b.c

diff --git a/06a.c b/06a.c
index 92a1b8a..c9152c6 100644 (file)
--- a/06a.c
+++ b/06a.c
@@ -7,28 +7,17 @@ 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];
-
+       long fish[9] = {0};
        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];
+               sum += fish[i];
        printf("%lu\n", sum);
 }
diff --git a/06b.c b/06b.c
index abc9892..0b2f076 100644 (file)
--- a/06b.c
+++ b/06b.c
@@ -7,28 +7,17 @@ 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];
-
+       long fish[9] = {0};
        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<256; 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<256; day++)
+               fish[(day+7) % 9] += fish[day%9];
 
        long sum = 0;
        for (long i = 0; i<=8; i++)
-               sum += this[i];
+               sum += fish[i];
        printf("%lu\n", sum);
 }