Advertisement
musifter

AoC 2022, day 2 (Perl - table version)

Dec 2nd, 2022
1,587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.79 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %table1 = (          # Rock      Paper     Scissors
  7.                 'A' => { 'X' => 4, 'Y' => 8, 'Z' => 3 },    # Rock
  8.                 'B' => { 'X' => 1, 'Y' => 5, 'Z' => 9 },    # Paper
  9.                 'C' => { 'X' => 7, 'Y' => 2, 'Z' => 6 },    # Scissors
  10.              );
  11.  
  12. my %table2 = (          # Lose      Draw      Win
  13.                 'A' => { 'X' => 3, 'Y' => 4, 'Z' => 8 },    # Rock
  14.                 'B' => { 'X' => 1, 'Y' => 5, 'Z' => 9 },    # Paper
  15.                 'C' => { 'X' => 2, 'Y' => 6, 'Z' => 7 },    # Scissors
  16.              );
  17.  
  18. my $part1 = 0;
  19. my $part2 = 0;
  20.  
  21. while (<>) {
  22.     my ($a, $b) = split;
  23.     $part1 += $table1{$a}{$b};
  24.     $part2 += $table2{$a}{$b};
  25. }
  26.  
  27. print "Part 1: $part1\n";
  28. print "Part 2: $part2\n";
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement