Advertisement
musifter

AoC 2023, day 2 (Perl)

Dec 2nd, 2023
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.67 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.16;
  4. use warnings;
  5.  
  6. use List::Util      qw(all max product);
  7.  
  8. # Number of cubes we want to test for:
  9. my %want = (red => 12, green => 13, blue => 14);
  10.  
  11. my $part1 = 0;
  12. my $part2 = 0;
  13.  
  14. foreach (<>) {
  15.     my ($game, $draws) = m#Game (\d+):(.*)#;
  16.  
  17.     # Get max seen of each type of cube in this game
  18.     my %max = (red => 0, green => 0, blue => 0);
  19.     foreach (split( /[,;]/, $draws )) {
  20.         my ($num, $colour) = m#(\d+) (\w+)#;
  21.         $max{$colour} = max( $max{$colour}, $num );
  22.     }
  23.  
  24.     $part1 += $game  if (all {$want{$_} >= $max{$_}} keys %want);
  25.     $part2 += product values %max;
  26. }
  27.  
  28. say "Part 1: $part1";
  29. say "Part 2: $part2";
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement