Advertisement
musifter

AoC 2022, day 2 (Perl)

Dec 2nd, 2022 (edited)
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.57 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $part1 = 0;
  7. my $part2 = 0;
  8.  
  9. while (<>) {
  10.     # convert input to majuscle ordinals
  11.     # Using %4 means that a ε [1,3] and b ε [0,2], so some added shifting needed
  12.     my ($a, $b) = map { ord($_) % 4 } split;
  13.  
  14.     # LDW is (b - (a-1) + 1) % 3 (+1 to shift to 0-2), move score is b + 1
  15.     $part1 += ($b - $a + 2) % 3 * 3 + $b + 1;
  16.  
  17.     # LWD is just 3 * b, move score is ((a-1) + b) mod 3, but with residue on [1,3]
  18.     $part2 += ($a + $b + 1) % 3 + 1 + 3 * $b;
  19. }
  20.  
  21. print "Part 1: $part1\n";
  22. print "Part 2: $part2\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement