#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)
{
}
f->buf = hex2bin[c];
}
- int r =*f->buf == '1' ? 1 : 0;
- f->buf++;
+ int r =*(f->buf++) == '1' ? 1 : 0;
f->pos++;
return r;
}
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));
}
{
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);
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));
}
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))