From 09f77b0ecadc0625753adfeb990142cf0c0f6735 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 16 Dec 2021 08:42:22 +0100 Subject: [PATCH] cleanup --- 16a.c | 19 +++++++++---------- 16b.c | 54 +++++++++++++++++++++--------------------------------- Makefile | 3 +-- 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/16a.c b/16a.c index 75b3404..658ec49 100644 --- a/16a.c +++ b/16a.c @@ -3,12 +3,14 @@ #include #include -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 --- 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 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)); } diff --git a/Makefile b/Makefile index 64234e9..7bd5349 100644 --- 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)) -- 2.20.1