Advertisement
musifter

AoC 2024, day 3, part 2 (Perl)

Dec 2nd, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.44 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7.  
  8. my $part2 = 0;
  9. my $do = 1;
  10.  
  11. foreach my $line (<>) {
  12.     while ($line =~ m#(do(n't)?\(\)|mul\(\d{1,3},\d{1,3}\))#g) {
  13.         if ($1 eq "do()") {
  14.             $do = 1;
  15.         } elsif ($1 eq "don't()") {
  16.             $do = 0;
  17.         } elsif ($do) {
  18.             $1 =~ m#(\d+),(\d+)#;
  19.             $part2 += $1 * $2;
  20.         }
  21.     }
  22. }
  23.  
  24. say "Part 2: ", $part2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement