Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/gawk -f
- BEGIN {
- logging=1; # Set to 0 to disable logging
- A["0"]="0000"; A["1"]="0001"; A["2"]="0010"; A["3"]="0011"; A["4"]="0100"; A["5"]="0101"; A["6"]="0110"; A["7"]="0111";
- A["8"]="1000"; A["9"]="1001"; A["A"]="1010"; A["B"]="1011"; A["C"]="1100"; A["D"]="1101"; A["E"]="1110"; A["F"]="1111";
- input = "8054F9C95F9C1C973D000D0A79F6635986270B054AE9EE51F8001D395CCFE21042497E4A2F6200E1803B0C20846820043630C1F8A840087C6C8BB1688018395559A30997A8AE60064D17980291734016100622F41F8DC200F4118D3175400E896C068E98016E00790169A600590141EE0062801E8041E800F1A0036C28010402CD3801A60053007928018CA8014400EF2801D359FFA732A000D2623CADE7C907C2C96F5F6992AC440157F002032CE92CE9352AF9F4C0119BDEE93E6F9C55D004E66A8B335445009E1CCCEAFD299AA4C066AB1BD4C5804149C1193EE1967AB7F214CF74752B1E5CEDC02297838C649F6F9138300424B9C34B004A63CCF238A56B71520142A5A7FC672E5E00B080350663B44F1006A2047B8C51CC80286C0055253951F98469F1D86D3C1E600F80021118A124261006E23C7E8260008641A8D51F0C01299EC3F4B6A37CABD80252211221A600BC930D0057B2FAA31CDCEF6B76DADF1666FE2E000FA4905CB7239AFAC0660114B39C9BA492D4EBB180252E472AD6C00BF48C350F9F47D2012B6C014000436284628BE00087C5D8671F27F0C480259C9FE16D1F4B224942B6F39CAF767931CFC36BC800EA4FF9CE0CCE4FCA4600ACCC690DE738D39D006A000087C2A89D0DC401987B136259006AFA00ACA7DBA53EDB31F9F3DBF31900559C00BCCC4936473A639A559BC433EB625404300564D67001F59C8E3172892F498C802B1B0052690A69024F3C95554C0129484C370010196269D071003A079802DE0084E4A53E8CCDC2CA7350ED6549CEC4AC00404D3C30044D1BA78F25EF2CFF28A60084967D9C975003992DF8C240923C45300BE7DAA540E6936194E311802D800D2CB8FC9FA388A84DEFB1CB2CBCBDE9E9C8803A6B00526359F734673F28C367D2DE2F3005256B532D004C40198DF152130803D11211C7550056706E6F3E9D24B0";
- split(input, ar, "");
- for (i in ar) { binary = binary A[ar[i]]; }
- pointer = 1;
- totalVNs = 0; # counter for part 1
- print "PART 2 : " readPacket(0)
- print "PART 1 : Total Version Numbers: " totalVNs;
- exit;
- }
- func readPacket(depth, maxLength, packetVersionDec, packetTypeIdDec, lengthTypeIdDec, lengthDec, subpacketCount, subpacketResult, i, result, initPointer) {
- depth ++;
- packetVersionDec = bin2dec(read(3));
- totalVNs += packetVersionDec;
- packetTypeIdDec = bin2dec(read(3));
- if (packetTypeIdDec != 4) { # operator
- lengthTypeIdDec = bin2dec(read(1));
- subpacketCount = 0;
- if (lengthTypeIdDec == 0) { # 15-bit nr
- # length is a 15-bit nr: the TOTAL LENGTH IN BITS of subpackets
- lengthDec = bin2dec(read(15));
- initPointer = pointer;
- while (pointer < initPointer + lengthDec) {
- subpacketCount ++;
- subpacketResult = readPacket(depth);
- arSub[depth][subpacketCount] = subpacketResult;
- }
- } else { # 11 bit number
- # length is a 11-bit nr: the NUMBER OF SUB-PACKETS CONTAINED
- subpacketCount = bin2dec(read(11));
- for (i=1; i <= subpacketCount; i++) {
- subpacketResult = readPacket(depth, 0);
- arSub[depth][i] = subpacketResult;
- }
- }
- result = 0;
- for (i=1; i<= subpacketCount; i++) { # Optimized for line count
- if (packetTypeIdDec == 0) { # SUM
- result += arSub[depth][i];
- } else if (packetTypeIdDec == 1) { # PRODUCT
- result = i==1 ? arSub[depth][i] : result * arSub[depth][i];
- } else if (packetTypeIdDec == 2) { # Minval
- result = (i==1 ? arSub[depth][i] : min(arSub[depth][i], result));
- } else if (packetTypeIdDec == 3) { # Maxval
- result = (i==1 ? arSub[depth][i] : max(arSub[depth][i], result));
- } else if (packetTypeIdDec == 5) { # GT
- result = arSub[depth][1] > arSub[depth][2] ? 1 : 0;
- } else if (packetTypeIdDec == 6) { # LT
- result = arSub[depth][1] < arSub[depth][2] ? 1 : 0;
- } else if (packetTypeIdDec == 7) { # EQ
- result = arSub[depth][1] == arSub[depth][2] ? 1 : 0;
- } else { print "KRAK - packetTypeIdDec " packetTypeIdDec " not recognized."; exit; }
- }
- } else { # literal
- literalDec = readLiteralPacketDec();
- result = literalDec;
- }
- return result;
- }
- func readLiteralPacketDec( result, nr) {
- do {
- notLast = bin2dec(read(1));
- nr = nr read(4);
- readCount += 5;
- } while (notLast);
- return bin2dec(nr);
- }
- func read(count, result) {
- result = substr(binary, pointer, count);
- pointer += count;
- if (pointer > length(binary) + 1) { print "KRAK, length ("length(binary)") exceeded"; exit; }
- return result;
- }
- func bin2dec(bin, i, tmpAr, result) {
- result = 0;
- split(bin, tmpAr, "");
- for (i in tmpAr) {
- result += 2^(length(tmpAr) - i) * (tmpAr[i] * 1);
- }
- return result * 1;
- }
- func min(a1, a2) { return (a1 < a2 ? a1 : a2); }
- func max(a1, a2) { return (a1 > a2 ? a1 : a2); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement