Advertisement
SadBunny

AOC 2021 Day 16 Part 1+2 AWK

Dec 16th, 2021 (edited)
2,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 4.51 KB | None | 0 0
  1. #!/bin/gawk -f
  2. BEGIN {
  3.     logging=1; # Set to 0 to disable logging
  4.  
  5.     A["0"]="0000"; A["1"]="0001"; A["2"]="0010"; A["3"]="0011"; A["4"]="0100"; A["5"]="0101"; A["6"]="0110"; A["7"]="0111";
  6.     A["8"]="1000"; A["9"]="1001"; A["A"]="1010"; A["B"]="1011"; A["C"]="1100"; A["D"]="1101"; A["E"]="1110"; A["F"]="1111";
  7.  
  8.     input = "8054F9C95F9C1C973D000D0A79F6635986270B054AE9EE51F8001D395CCFE21042497E4A2F6200E1803B0C20846820043630C1F8A840087C6C8BB1688018395559A30997A8AE60064D17980291734016100622F41F8DC200F4118D3175400E896C068E98016E00790169A600590141EE0062801E8041E800F1A0036C28010402CD3801A60053007928018CA8014400EF2801D359FFA732A000D2623CADE7C907C2C96F5F6992AC440157F002032CE92CE9352AF9F4C0119BDEE93E6F9C55D004E66A8B335445009E1CCCEAFD299AA4C066AB1BD4C5804149C1193EE1967AB7F214CF74752B1E5CEDC02297838C649F6F9138300424B9C34B004A63CCF238A56B71520142A5A7FC672E5E00B080350663B44F1006A2047B8C51CC80286C0055253951F98469F1D86D3C1E600F80021118A124261006E23C7E8260008641A8D51F0C01299EC3F4B6A37CABD80252211221A600BC930D0057B2FAA31CDCEF6B76DADF1666FE2E000FA4905CB7239AFAC0660114B39C9BA492D4EBB180252E472AD6C00BF48C350F9F47D2012B6C014000436284628BE00087C5D8671F27F0C480259C9FE16D1F4B224942B6F39CAF767931CFC36BC800EA4FF9CE0CCE4FCA4600ACCC690DE738D39D006A000087C2A89D0DC401987B136259006AFA00ACA7DBA53EDB31F9F3DBF31900559C00BCCC4936473A639A559BC433EB625404300564D67001F59C8E3172892F498C802B1B0052690A69024F3C95554C0129484C370010196269D071003A079802DE0084E4A53E8CCDC2CA7350ED6549CEC4AC00404D3C30044D1BA78F25EF2CFF28A60084967D9C975003992DF8C240923C45300BE7DAA540E6936194E311802D800D2CB8FC9FA388A84DEFB1CB2CBCBDE9E9C8803A6B00526359F734673F28C367D2DE2F3005256B532D004C40198DF152130803D11211C7550056706E6F3E9D24B0";
  9.  
  10.     split(input, ar, "");
  11.     for (i in ar) { binary = binary A[ar[i]]; }
  12.  
  13.     pointer = 1;
  14.     totalVNs = 0; # counter for part 1
  15.  
  16.     print "PART 2 : " readPacket(0)
  17.  
  18.     print "PART 1 : Total Version Numbers: " totalVNs;
  19.     exit;
  20. }
  21.  
  22. func readPacket(depth, maxLength,      packetVersionDec, packetTypeIdDec, lengthTypeIdDec, lengthDec, subpacketCount, subpacketResult, i, result, initPointer) {
  23.     depth ++;
  24.     packetVersionDec = bin2dec(read(3));
  25.     totalVNs += packetVersionDec;
  26.     packetTypeIdDec = bin2dec(read(3));
  27.     if (packetTypeIdDec != 4) { # operator
  28.         lengthTypeIdDec = bin2dec(read(1));
  29.         subpacketCount = 0;
  30.         if (lengthTypeIdDec == 0) { # 15-bit nr
  31.             # length is a 15-bit nr: the TOTAL LENGTH IN BITS of subpackets
  32.             lengthDec = bin2dec(read(15));
  33.             initPointer = pointer;
  34.             while (pointer < initPointer + lengthDec) {
  35.                 subpacketCount ++;
  36.                 subpacketResult = readPacket(depth);
  37.                 arSub[depth][subpacketCount] = subpacketResult;
  38.             }
  39.         } else { # 11 bit number
  40.             # length is a 11-bit nr: the NUMBER OF SUB-PACKETS CONTAINED
  41.             subpacketCount = bin2dec(read(11));
  42.             for (i=1; i <= subpacketCount; i++) {
  43.                 subpacketResult = readPacket(depth, 0);
  44.                 arSub[depth][i] = subpacketResult;
  45.             }
  46.         }
  47.         result = 0;
  48.         for (i=1; i<= subpacketCount; i++) { # Optimized for line count
  49.             if (packetTypeIdDec == 0) { # SUM
  50.                 result += arSub[depth][i];
  51.             } else if (packetTypeIdDec == 1) { # PRODUCT
  52.                 result = i==1 ? arSub[depth][i] : result * arSub[depth][i];
  53.             } else if (packetTypeIdDec == 2) { # Minval
  54.                 result = (i==1 ? arSub[depth][i] : min(arSub[depth][i], result));
  55.             } else if (packetTypeIdDec == 3) { # Maxval
  56.                 result = (i==1 ? arSub[depth][i] : max(arSub[depth][i], result));
  57.             } else if (packetTypeIdDec == 5) { # GT
  58.                 result = arSub[depth][1] > arSub[depth][2] ? 1 : 0;
  59.             } else if (packetTypeIdDec == 6) { # LT
  60.                 result = arSub[depth][1] < arSub[depth][2] ? 1 : 0;
  61.             } else if (packetTypeIdDec == 7) { # EQ
  62.                 result = arSub[depth][1] == arSub[depth][2] ? 1 : 0;
  63.             } else { print "KRAK - packetTypeIdDec " packetTypeIdDec " not recognized."; exit; }
  64.         }
  65.            
  66.     } else { # literal
  67.         literalDec = readLiteralPacketDec();
  68.         result = literalDec;
  69.     }
  70.     return result;
  71. }
  72.  
  73. func readLiteralPacketDec(      result, nr) {
  74.     do {
  75.         notLast = bin2dec(read(1));
  76.         nr = nr read(4);
  77.         readCount += 5;
  78.     } while (notLast);
  79.     return bin2dec(nr);
  80. }
  81.  
  82. func read(count,    result) {
  83.     result = substr(binary, pointer, count);
  84.     pointer += count;
  85.     if (pointer > length(binary) + 1) { print "KRAK, length ("length(binary)") exceeded"; exit; }
  86.     return result;
  87. }
  88.  
  89. func bin2dec(bin,    i, tmpAr, result) {
  90.     result = 0;
  91.     split(bin, tmpAr, "");
  92.     for (i in tmpAr) {
  93.         result += 2^(length(tmpAr) - i) * (tmpAr[i] * 1);
  94.     }
  95.     return result * 1;
  96. }
  97.  
  98. func min(a1, a2) { return (a1 < a2 ? a1 : a2); }
  99. func max(a1, a2) { return (a1 > a2 ? a1 : a2); }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement