Advertisement
SadBunny

AOC 2022 Day 2 Part 1+2 AWK

Dec 2nd, 2022
1,435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 0.38 KB | None | 0 0
  1. # Part 1
  2. BEGIN { thing="A XB YC ZA YB ZC X"; }
  3. { total += index(thing, substr($0, 3, 1)) / 3 + (int((index(thing, $0) + 8) / 9)) * 3; }
  4. END { print total; }
  5.  
  6. # Part 2
  7. BEGIN { scores="ABC"; outcomes="XYZ"; }
  8.  
  9. {
  10.         i = index(outcomes, substr($0, 3, 1));
  11.         total += (index(scores, substr($0, 1, 1)) - 1 + ((i + 1) % 3)) % 3 + 1 + (i - 1) * 3;
  12. }
  13.  
  14. END { print total; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement