day 6
authorMart Lubbers <mart@martlubbers.net>
Mon, 6 Dec 2021 06:00:24 +0000 (07:00 +0100)
committerMart Lubbers <mart@martlubbers.net>
Mon, 6 Dec 2021 06:00:24 +0000 (07:00 +0100)
03b.c
06.txt [new file with mode: 0644]
06a.c [new file with mode: 0644]
06b.c [new file with mode: 0644]
06e.txt [new file with mode: 0644]
Makefile

diff --git a/03b.c b/03b.c
index c940822..2213374 100644 (file)
--- a/03b.c
+++ b/03b.c
@@ -35,16 +35,14 @@ int main(void)
        unsigned numbers[2000] = {0};
        int c = 0, y = 0, maxbits = 0, bits = 0;
        while ((c = getchar()) != EOF) {
-               if (c == '0') {
-                       numbers[y] = numbers[y]*2;
-                       bits++;
-               } else if (c == '1') {
-                       numbers[y] = numbers[y]*2+1;
-                       bits++;
-               } else if (c == '\n') {
+               if (c == '\n') {
                        maxbits = bits > maxbits ? bits : maxbits;
                        bits = 0;
                        y++;
+               } else {
+                       numbers[y] *= 2;
+                       if (c == '1')
+                               numbers[y]++;
                }
        }
 
diff --git a/06.txt b/06.txt
new file mode 100644 (file)
index 0000000..b800e1c
--- /dev/null
+++ b/06.txt
@@ -0,0 +1 @@
+1,5,5,1,5,1,5,3,1,3,2,4,3,4,1,1,3,5,4,4,2,1,2,1,2,1,2,1,5,2,1,5,1,2,2,1,5,5,5,1,1,1,5,1,3,4,5,1,2,2,5,5,3,4,5,4,4,1,4,5,3,4,4,5,2,4,2,2,1,3,4,3,2,3,4,1,4,4,4,5,1,3,4,2,5,4,5,3,1,4,1,1,1,2,4,2,1,5,1,4,5,3,3,4,1,1,4,3,4,1,1,1,5,4,3,5,2,4,1,1,2,3,2,4,4,3,3,5,3,1,4,5,5,4,3,3,5,1,5,3,5,2,5,1,5,5,2,3,3,1,1,2,2,4,3,1,5,1,1,3,1,4,1,2,3,5,5,1,2,3,4,3,4,1,1,5,5,3,3,4,5,1,1,4,1,4,1,3,5,5,1,4,3,1,3,5,5,5,5,5,2,2,1,2,4,1,5,3,3,5,4,5,4,1,5,1,5,1,2,5,4,5,5,3,2,2,2,5,4,4,3,3,1,4,1,2,3,1,5,4,5,3,4,1,1,2,2,1,2,5,1,1,1,5,4,5,2,1,4,4,1,1,3,3,1,3,2,1,5,2,3,4,5,3,5,4,3,1,3,5,5,5,5,2,1,1,4,2,5,1,5,1,3,4,3,5,5,1,4,3
diff --git a/06a.c b/06a.c
new file mode 100644 (file)
index 0000000..92a1b8a
--- /dev/null
+++ b/06a.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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 *p = strtok(buf, ",");
+       this[atoi(p)]++;
+       while ((p = strtok(NULL, ",")) != NULL)
+               this[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;
+       }
+
+       long sum = 0;
+       for (long i = 0; i<=8; i++)
+               sum += this[i];
+       printf("%lu\n", sum);
+}
diff --git a/06b.c b/06b.c
new file mode 100644 (file)
index 0000000..abc9892
--- /dev/null
+++ b/06b.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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 *p = strtok(buf, ",");
+       this[atoi(p)]++;
+       while ((p = strtok(NULL, ",")) != NULL)
+               this[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;
+       }
+
+       long sum = 0;
+       for (long i = 0; i<=8; i++)
+               sum += this[i];
+       printf("%lu\n", sum);
+}
diff --git a/06e.txt b/06e.txt
new file mode 100644 (file)
index 0000000..55129f1
--- /dev/null
+++ b/06e.txt
@@ -0,0 +1 @@
+3,4,3,1,2
index dfdd641..5da4afc 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 CFLAGS:=-Wall -Wextra -O3
 LFLAGS:=-f
 
-BINARIES:=$(foreach num,$(shell seq -f '%02.0f' 1 5),$(num)a $(num)b)
+BINARIES:=$(foreach num,$(shell seq -f '%02.0f' 1 6),$(num)a $(num)b)
 
 all: $(BINARIES)