:day 3
authorMart Lubbers <mart@martlubbers.net>
Fri, 3 Dec 2021 08:47:40 +0000 (09:47 +0100)
committerMart Lubbers <mart@martlubbers.net>
Fri, 3 Dec 2021 08:53:02 +0000 (09:53 +0100)
03a.c [new file with mode: 0644]
03b.c [new file with mode: 0644]
3.txt [new file with mode: 0644]
3e.txt [new file with mode: 0644]
Makefile

diff --git a/03a.c b/03a.c
new file mode 100644 (file)
index 0000000..b11cc02
--- /dev/null
+++ b/03a.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+
+int main(void)
+{
+       int bits[100] = {0};
+       int i = 0, maxi = 0, c;
+       while ((c = getchar()) != EOF) {
+               if (c == '0')
+                       bits[i++] -= 1;
+               else if (c == '1')
+                       bits[i++] += 1;
+               else if (c == '\n')
+                       i = 0;
+               maxi = i > maxi ? i : maxi;
+       }
+
+       int gamma = 0, epsilon = 0;
+       for (int j = 0; j<maxi; j++) {
+               gamma *= 2;
+               epsilon *= 2;
+               if (bits[j] > 0)
+                       gamma++;
+               else
+                       epsilon++;
+       }
+       printf("%d\n", gamma*epsilon);
+       return 0;
+}
diff --git a/03b.c b/03b.c
new file mode 100644 (file)
index 0000000..c940822
--- /dev/null
+++ b/03b.c
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+#define bitread(value, bit) (((value) >> (bit)) & 0x01)
+
+int incremental_search(unsigned numbers[], int len, int bits, bool inv)
+{
+       //Copy for the initial search
+       int cur[len];
+       int curlen;
+       for (curlen = 0; curlen<len; curlen++)
+               cur[curlen] = numbers[curlen];
+
+       for (int bit = bits-1; bit >= 0 || curlen > 1; bit--) {
+               //Determine 0 1 distribution
+               int dist = 0;
+               for (int i = 0; i<curlen; i++)
+                       dist += bitread(cur[i], bit) == 0 ? -1 : 1;
+
+               dist = dist >= 0;
+               dist = inv ? 1-dist : dist;
+
+               //Filter out numbers
+               int newcurlen = 0;
+               for (int i = 0; i<curlen; i++)
+                       if (bitread(cur[i], bit) == dist)
+                               cur[newcurlen++] = cur[i];
+               curlen = newcurlen;
+       }
+       return cur[0];
+}
+
+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') {
+                       maxbits = bits > maxbits ? bits : maxbits;
+                       bits = 0;
+                       y++;
+               }
+       }
+
+       int oxygen = incremental_search(numbers, y, maxbits, false);
+       int co2 = incremental_search(numbers, y, maxbits, true);
+
+       printf("%d\n", oxygen*co2);
+       return 0;
+}
diff --git a/3.txt b/3.txt
new file mode 100644 (file)
index 0000000..f398604
--- /dev/null
+++ b/3.txt
@@ -0,0 +1,1000 @@
+010111111011
+010010101110
+011001001100
+001000001010
+111100101000
+111010101100
+000111101111
+010011010011
+100010111011
+101011000111
+100111101010
+101101101101
+110010110110
+100110011100
+001110011000
+011000101010
+001100111101
+100011101111
+100111011001
+011100101101
+111101000111
+111000101011
+001001000101
+010110011000
+110100100001
+010010010011
+100100100100
+011011001000
+111101011101
+101011110011
+110011101101
+001001000100
+100111101110
+101101101010
+111110101000
+111011011001
+111110101101
+110101010100
+011100110000
+000010111110
+111011111111
+010111111110
+101001110101
+001111010100
+001111110001
+000010000000
+010001101000
+100001001111
+101111000010
+000011110001
+110111101110
+000000100111
+010100000111
+011111100001
+100011110001
+101000001111
+101010111001
+001101100100
+001100111001
+001000011010
+001001111100
+011001011001
+101011001010
+100101101101
+000101011110
+101100110111
+011000010110
+110101000110
+011010100101
+110011100110
+111101000010
+111110101011
+110101100101
+101110101010
+100110001011
+000110101100
+010100001001
+010011001110
+101010010101
+101010000101
+100100001011
+010010100101
+011000111111
+001010000001
+000001100111
+010101101111
+101110000110
+100101001001
+010000101001
+001101011001
+011101111110
+011110011100
+110100110111
+000101011010
+100000010011
+011100111000
+001110101000
+001001001000
+110101011101
+000011001111
+101000011010
+100011110010
+100100010111
+110001010100
+111010100110
+101001010100
+110100111101
+111000010111
+010001111000
+000101010110
+010100000100
+001000110010
+100111001100
+111001000100
+011100001011
+100110001010
+000010100111
+001010111110
+101100110001
+010100100001
+011001110010
+010011100101
+011111010110
+111000010010
+011110110101
+110101101100
+101010110110
+000111000001
+001111111101
+110011101010
+011000110010
+101110101011
+110100001100
+001000011000
+110110110010
+101111000111
+000111111001
+111100110100
+010010011010
+010010111010
+000001101000
+000101010101
+110111110101
+111001110110
+110011001100
+011110011001
+000010110101
+011011000111
+010101010110
+001011010100
+100001111001
+011000111010
+011100010000
+110001011010
+110010110111
+001101010101
+000110011000
+000010011110
+101100111001
+100011001110
+100010000000
+001111010001
+011100010011
+010001000010
+110010010111
+011011111101
+010011010100
+100001101001
+101100110110
+000000101011
+111000110111
+111101010110
+111110000000
+101010000111
+011010110110
+100110100111
+001011001011
+110011111000
+010111100110
+001100101010
+110001001000
+000100000111
+000001011100
+000100000000
+100010100000
+110001011011
+110110111101
+100110010001
+001101001110
+011111011000
+110010000100
+100011010110
+011001100000
+010101001110
+111100000001
+110100010111
+000001000011
+101100010011
+000100011111
+111000001001
+101100011100
+010110010001
+000110000010
+110000101111
+011010011100
+111000011110
+100110110001
+100000011100
+101010001011
+010111001100
+100111110111
+111100001011
+010100100100
+100111100101
+111100000111
+101001001101
+111001111000
+001110010100
+111010011001
+010100111111
+110111010011
+100100000001
+001101111111
+111101001100
+000111101010
+000100101001
+111011011101
+010001110111
+111010100001
+001101010100
+101010011100
+000011100100
+100101101010
+011111100000
+011011001110
+000101000111
+010010111111
+001100111011
+000001101001
+001110110001
+010011011110
+001011011000
+100000111010
+001000100101
+010001100011
+011001000111
+010110101010
+101000001001
+110101011000
+001100000101
+100110011001
+000011001001
+100110111001
+111101101001
+011011001001
+010001100111
+001100011100
+011111101111
+100100111010
+101111110011
+110111001110
+000111100000
+011110000101
+111110100010
+001011001101
+110100101011
+011100001101
+111001111100
+111110100110
+000010010100
+010111110010
+001110001101
+011111010011
+110110111010
+101001110000
+011110110011
+000111011000
+010111000110
+001011100010
+001110000010
+010111000011
+010011111110
+011000010000
+101111011110
+101010000110
+011100011101
+011101101100
+100110101001
+000000011001
+100101010010
+110011010101
+010100100111
+011101011100
+011001111001
+000001001000
+001000101011
+010110111101
+100110010010
+010010010110
+000000111001
+011111110101
+011111001000
+100100011111
+000001111000
+111101010010
+101010100100
+011010111101
+000001011011
+101111100001
+100011000000
+110011001010
+000100010111
+100000010000
+101111010110
+010010010001
+101011100001
+101001011100
+001111000011
+111000100010
+001101110110
+001110111011
+100001110010
+000001110001
+111100001000
+100010011101
+000011000110
+111100100110
+000111110111
+000100111001
+011001110001
+111101010001
+110110101100
+010001011001
+001101111110
+000101111010
+110100100110
+111011001110
+110011010100
+010000100011
+011000111001
+110001110001
+101000000001
+101011000101
+011010000110
+000010111000
+010011100011
+111101101110
+001111010010
+100011011100
+111110111010
+000010110010
+101000011100
+010011101011
+110001011000
+000000010011
+001101010010
+110111111011
+111101011100
+111001010011
+110111100101
+111000111001
+001110010010
+001011101101
+110100100111
+101011100000
+011000100101
+110101010010
+000110000111
+111111001000
+000011100110
+101101110011
+001110011111
+110000101110
+110011100101
+101110100101
+001000111000
+101111000011
+100111111011
+011010101111
+101101001000
+000101101110
+111101000011
+000011000010
+000101010010
+101001011101
+110011010011
+111010010001
+110100010001
+010100001010
+110111101010
+100111000101
+101001100010
+011010011011
+101011101101
+011001100001
+001000110111
+000100101000
+010000000111
+111111110000
+001011110110
+100010010001
+100110101111
+110110010000
+100110011011
+001100110101
+100011101100
+000011011001
+010110100001
+011110101111
+101011011000
+101110111001
+111000010000
+011100000010
+100110100101
+010101011111
+100000111001
+011000111100
+101100100111
+101011111000
+111101111101
+101000100011
+101000010101
+111001010010
+101110000111
+101010000001
+000100100111
+100001011101
+110010101010
+000000100100
+011101000101
+110010001101
+111011101110
+111111111110
+011100000111
+101111110010
+000101100110
+000110001011
+100100011000
+010001101111
+011110010000
+100011011111
+010101101010
+000011100000
+000101001110
+001001110001
+111110011001
+101110000000
+010001010010
+110001000110
+101011001110
+010001101001
+000000011011
+101001010011
+011010000100
+001100001100
+100010001001
+100000111111
+001000000000
+100110110100
+100111110000
+101001110001
+000110101101
+000111001010
+101100011011
+110001101000
+000111101110
+110110010011
+100010001000
+001010001100
+001011111110
+001110000111
+110010100000
+100001110101
+000110111101
+110010011000
+111110111011
+101000000011
+011010011110
+110000001111
+011000110100
+111111001101
+000010110100
+101100001100
+110000101011
+010000001110
+101001101000
+011000000100
+101101011000
+100111111100
+110001001010
+001010101100
+111010001110
+101110100110
+011110010001
+111011111010
+000001100110
+010001100101
+100101001111
+111001001011
+111101101010
+011011000001
+010111010101
+110001111110
+110100101110
+111101011011
+011000101011
+010100111100
+011000000111
+101000101001
+111000110001
+101000110001
+001111010011
+100000010100
+001001101101
+010101110001
+100110000101
+011000101101
+001110110010
+100111000001
+011001000001
+000101101000
+101000101110
+000100001110
+111110101111
+011110110001
+101001110010
+000000111010
+101111010010
+011110011010
+101000011111
+000100001100
+110001111001
+110000110101
+111010011100
+001000111011
+111110001111
+011011011000
+000110110001
+101001010010
+111010100011
+000001111100
+110010000001
+110101111011
+100011001001
+010011100001
+101101011111
+100000001010
+001011001000
+000011100010
+011110111000
+000110111011
+010111001011
+011100011000
+101010011011
+001001011110
+000110001010
+111011010101
+010101110010
+011100101001
+100000110010
+111100111001
+001011011101
+110101101001
+001000111110
+101011000011
+001110111111
+100101010110
+111011011110
+010111111001
+011001010001
+110101111111
+110100100101
+111111110001
+010011000101
+011101101010
+101111000100
+011101000100
+001110110110
+010011011000
+000011011010
+110000000110
+001100001101
+111001111001
+100001100101
+100100010011
+111011111000
+001001111110
+101101000000
+000001100001
+001100001011
+110011011111
+010101111110
+001110011100
+111111000100
+010011000010
+101100010000
+000010110111
+110100000101
+110100010010
+111001100101
+010000010001
+101100100110
+001111101100
+011100110110
+000000111101
+001000000100
+010101101101
+100101101000
+001001010101
+000000001111
+110111110100
+000010110110
+001000000010
+011000101000
+100101001101
+010110101110
+100010110001
+010111010000
+001111011001
+000011110100
+100100111000
+011011100110
+100100010110
+000001111101
+010111011011
+110001000101
+111100010001
+101001000111
+110100010000
+001010000111
+101001110111
+010010010010
+100010111100
+001101000011
+011001110011
+000100101111
+010110011101
+001001011010
+111011101001
+111111000000
+111110000111
+110000001011
+010000110010
+011100011001
+011001100011
+001101000110
+011001011111
+100111100000
+110111010100
+101101000001
+110011101000
+001010110101
+100101010001
+101000101100
+010110001000
+111011111011
+000010010001
+010111011010
+011101101011
+001000101110
+101010100111
+001011000111
+011011010110
+110111101111
+100001110001
+100100101111
+110000010011
+011110111011
+000110000100
+110000111111
+100110101100
+001101110101
+011100000100
+001100011110
+010111010010
+001110101101
+100110001110
+100100010000
+001000001101
+011101110011
+011001100110
+110101000011
+100101001010
+111111101111
+010011000100
+100111000111
+101100101100
+010110000100
+111100000100
+000000101001
+000011110010
+011011110111
+011101110001
+111110000001
+011000011001
+100001101111
+110000011010
+110110101010
+110100110000
+111111000011
+100011100010
+100100010010
+110100011000
+000110000110
+001101001000
+101011110111
+000011110111
+001001100100
+100010110011
+100111111000
+000001111010
+110001111101
+100011111111
+111001001101
+011111101011
+101111110111
+100111000011
+010010001010
+010101101011
+011011110000
+110111000000
+010011101111
+010001010101
+111110101010
+001010101010
+001100101101
+111011000111
+101001000001
+000101000100
+010011110010
+001110100010
+110111001000
+111011001101
+111110001010
+110000100001
+000000010101
+000010011001
+011100100111
+001101111010
+000001100100
+011111101100
+110100011111
+010110000000
+001010101001
+000010010011
+111100000110
+110101000101
+010110001011
+111001001000
+010110101111
+001000000001
+111100001101
+001000110110
+110001101001
+001110001011
+111101101011
+011101111100
+100011001011
+001000001001
+000001001100
+100100110111
+111111101101
+001110100000
+001001001001
+001111001100
+101100101111
+101100110010
+101000100001
+101000010110
+100000010101
+011010011000
+101101101110
+011101001100
+101011111100
+101110101000
+110010100100
+011111011110
+110010011101
+110100100011
+111111100100
+011011011101
+011100100001
+101011110000
+001100100011
+000111111000
+110010001010
+100001001001
+110010001000
+111010001001
+010110111111
+101011001101
+101010011110
+011011110001
+101010100011
+100000110111
+100011101001
+101110010011
+101011011011
+110010010000
+011110100011
+010010110110
+100110010111
+110100000110
+101011101011
+001100010100
+101101010000
+110110001001
+010010001101
+111011010110
+111100001110
+000101111001
+100001000001
+101111101001
+100101101110
+000011001101
+000001000110
+111110001011
+000010101111
+011101101101
+000000000110
+111111101010
+101001110110
+111011000100
+001011101111
+110010111010
+110011110001
+111100011111
+110110010001
+100101110000
+010001001011
+101100010100
+110100001010
+001011101001
+110110000001
+001101001001
+100000111100
+001101101010
+010000110011
+011111101110
+110001110011
+011110011000
+000010001010
+001110000100
+000110101111
+100011111000
+100101100010
+111111110010
+101100011001
+011100100000
+100001100011
+001010011110
+111100011010
+010110100010
+101110010010
+001011110010
+100010111000
+001100110010
+001001101001
+011111101101
+101100111100
+011010011101
+101011100100
+111000001000
+010111011100
+001010000000
+011110000110
+110101101110
+000111000101
+101001011010
+110010100010
+100101110010
+000101100100
+010100110010
+011001000010
+110010011010
+010100001011
+011001111110
+011000101001
+011101011011
+010010000111
+000001110111
+100010101001
+111100011101
+100010001011
+111100101100
+011111010101
+111100101110
+111101001110
+111000010110
+000000110110
+011110000001
+000111011110
+111010111010
+101010001101
+111110101100
+000111110010
+110000111101
+111101110101
+101010101100
+101110110010
+110110011010
+111110110001
+011101111011
+101011111011
+100111101101
+000110011001
+100101101011
+110101101101
+010111110100
+001111110010
+101010101000
+011001100100
+101011111111
+001001000001
+010100100110
+111100011100
+011100011010
+000011011110
+001011101010
+010010001110
+101101101111
+000101000010
+010111100010
+010010010000
+111000000101
+010001100010
+101100001111
+011110111100
+100011000010
+010100001111
+011100101110
+011000111110
+111101010000
+110100111000
+001010001111
+111100011011
+101000111110
+100000011011
+010110000001
+001111100111
+001111101111
+011001011100
+111001000011
+110011101111
+001101100011
+101100111111
+011111101010
+010000011110
+001111111111
+010001001001
+111010101000
+110100001001
+111111010111
+000101110100
+101101100000
+001101100001
+001111111000
+100111101001
+000110010001
+100011101011
+000010000101
+101010001100
+000111110011
+001010110001
+111110001110
+010011100010
+100011010000
+100001010010
+100011000101
+110011000100
+010000010000
+110100110011
+100011000111
+010111101010
+101011000010
+110000101100
+111100001111
+000110100001
+010010110001
+111000100110
+000110111110
+100101000000
+011101001010
+101111011011
+111011101111
+000000010100
+110011101110
+111001110111
+011111000111
+111111011100
+110010011111
+111111110111
+001110110011
+000010010010
+100100100001
+111010000101
+001001110011
+101001101111
+101110101110
+101000101111
diff --git a/3e.txt b/3e.txt
new file mode 100644 (file)
index 0000000..a6366a8
--- /dev/null
+++ b/3e.txt
@@ -0,0 +1,12 @@
+00100
+11110
+10110
+10111
+10101
+01111
+00111
+11100
+10000
+11001
+00010
+01010
index b275854..2f07760 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
-CFLAGS=-Wall -Wextra
+CFLAGS:=-Wall -Wextra -O3
+LFLAGS:=-f
 
-BINARIES:=01 02a 02b
+BINARIES:=01 02a 02b 03a 03b
 
 all: $(BINARIES)