Advertisement
SadBunny

AOC 2022 Day 4 parts 1+2 (AWK)

Dec 4th, 2022
1,426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 0.78 KB | Source Code | 0 0
  1. #### PART 1 ####
  2. {
  3.         split($0, ar, ",");
  4.         left = nrSeq(ar[1]); right = nrSeq(ar[2]);
  5.         if (left ~ right || right ~ left) { result ++; }
  6. }
  7.  
  8. END { print result; }
  9.  
  10. func nrSeq(seq,    i,result, ar) {
  11.         split(seq, ar, "-");
  12.         for (i=ar[1]; i<=ar[2]; i++) {result =result "L" i "R"; }
  13.         return result;
  14. }
  15.  
  16. #### PART 2 ####
  17. {
  18.         split($0, ar, ",");
  19.         delete arLeft; fillAr(arLeft, ar[1]);
  20.         delete arRight; fillAr(arRight, ar[2]);
  21.         for (i in arLeft) {
  22.                 if (i in arRight) { result ++; break; }
  23.         }
  24. }
  25.  
  26. END { print result; }
  27.  
  28. func fillAr(ar, seq,   i) {
  29.         split(seq, tmpAr, "-");
  30.         for (i=tmpAr[1]; i<=tmpAr[2]; i++) {
  31.                 ar[i] = 1;
  32.         }
  33.         return r;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement