cleanup
authorMart Lubbers <mart@martlubbers.net>
Thu, 16 Dec 2021 07:42:22 +0000 (08:42 +0100)
committerMart Lubbers <mart@martlubbers.net>
Thu, 16 Dec 2021 07:43:12 +0000 (08:43 +0100)
16a.c
16b.c
Makefile

diff --git a/16a.c b/16a.c
index 75b3404..658ec49 100644 (file)
--- a/16a.c
+++ b/16a.c
@@ -3,12 +3,14 @@
 #include <string.h>
 #include <stdint.h>
 
-char *hex2bin[] = { ['0'] = "0000" , ['1'] = "0001" , ['2'] = "0010" , ['3'] = "0011" , ['4'] = "0100" , ['5'] = "0101" , ['6'] = "0110" , ['7'] = "0111" , ['8'] = "1000" , ['9'] = "1001" , ['A'] = "1010" , ['B'] = "1011" , ['C'] = "1100" , ['D'] = "1101" , ['E'] = "1110" , ['F'] = "1111", ['\n'] = "0000"};
+char *hex2bin[] =
+       { ['0'] = "0000", ['1'] = "0001", ['2'] = "0010", ['3'] = "0011"
+       , ['4'] = "0100", ['5'] = "0101", ['6'] = "0110", ['7'] = "0111"
+       , ['8'] = "1000", ['9'] = "1001", ['A'] = "1010", ['B'] = "1011"
+       , ['C'] = "1100", ['D'] = "1101", ['E'] = "1110", ['F'] = "1111"
+       , ['\n'] = "0000"};
 
-struct stream {
-       int pos;
-       char *buf;
-};
+struct stream { int pos; char *buf; };
 
 int next(struct stream *f)
 {
@@ -20,8 +22,7 @@ int next(struct stream *f)
                }
                f->buf = hex2bin[c];
        }
-       int r =*f->buf == '1' ? 1 : 0;
-       f->buf++;
+       int r =*(f->buf++) == '1' ? 1 : 0;
        f->pos++;
        return r;
 }
@@ -72,7 +73,5 @@ int parse_packet(struct stream *f)
 int main()
 {
        struct stream f = {.pos=0, .buf=""};
-
-       int r = parse_packet(&f);
-       printf("r: %d\n", r);
+       printf("r: %d\n", parse_packet(&f));
 }
diff --git a/16b.c b/16b.c
index 4310a7b..437560e 100644 (file)
--- a/16b.c
+++ b/16b.c
@@ -38,13 +38,13 @@ unsigned long parse_packet(struct stream *f)
 {
        int packetversion  __attribute__((unused)) = bin2int(f, 3);
        int packettype = bin2int(f, 3);
-       unsigned long result = 0;
+       unsigned long r = 0;
 
        //literal
        if (packettype == 4) {
                while (next(f) == 1)
-                       result = result*16+bin2int(f, 4);
-               result = result*16+bin2int(f, 4);
+                       r = r*16+bin2int(f, 4);
+               r = r*16+bin2int(f, 4);
        //operator
        } else {
                int lengthtypeid = next(f);
@@ -64,45 +64,33 @@ unsigned long parse_packet(struct stream *f)
                                packets[i] = parse_packet(f);
                }
 
-               //sum
-               if (packettype == 0) {
+               if (packettype == 0) {        //sum
                        for (int i = 0; i<npackets; i++)
-                               result += packets[i];
-               //product
-               } else if (packettype == 1) {
-                       result = 1;
+                               r += packets[i];
+               } else if (packettype == 1) { //product
+                       r = 1;
                        for (int i = 0; i<npackets; i++)
-                               result *= packets[i];
-               //minimum
-               } else if (packettype == 2) {
-                       result = ULONG_MAX;
+                               r *= packets[i];
+               } else if (packettype == 2) { //minimum
+                       r = ULONG_MAX;
                        for (int i = 0; i<npackets; i++)
-                               result = packets[i] < result ? packets[i] : result;
-               //maximum
-               } else if (packettype == 3) {
-                       result = 0;
+                               r = packets[i] < r ? packets[i] : r;
+               } else if (packettype == 3) { //maximum
                        for (int i = 0; i<npackets; i++)
-                               result = packets[i] > result ? packets[i] : result;
-               //greater than
-               } else if (packettype == 5) {
-                       result = packets[0] > packets[1];
-               //less than
-               } else if (packettype == 6) {
-                       result = packets[0] < packets[1];
-               //equal to
-               } else if (packettype == 7) {
-                       result = packets[0] == packets[1];
-               //unknown
-               } else {
-                       fprintf(stderr, "unknown operator: %d\n", packettype);
-                       exit(1);
+                               r = packets[i] > r ? packets[i] : r;
+               } else if (packettype == 5) { //greater than
+                       r = packets[0] > packets[1];
+               } else if (packettype == 6) { //less than
+                       r = packets[0] < packets[1];
+               } else if (packettype == 7) { //equal to
+                       r = packets[0] == packets[1];
                }
        }
-       return result;
+       return r;
 }
 
 int main()
 {
        struct stream f = {.pos=0, .buf=""};
-       printf("r: %lu\n", parse_packet(&f));
+       printf("%lu\n", parse_packet(&f));
 }
index 64234e9..7bd5349 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,14 +1,13 @@
 CFLAGS:=-Wall -Wextra -O3
 LFLAGS:=-f
 
-BINARIES:=$(foreach num,$(shell seq -f '%02.0f' 1 14),$(num)a $(num)b)
+BINARIES:=$(foreach num,$(shell seq -f '%02.0f' 1 16),$(num)a $(num)b)
 
 all: $(BINARIES)
 
 clean:
        $(RM) *.o a.out $(BINARIES)
 
-05%: CFLAGS+=-DHASH_BLOOM
 06b: LDLIBS+=-lgmp
 
 run: $(addprefix run_,$(BINARIES))