musifter

AoC 2025, day 2, Regex (Perl)

Dec 2nd, 2025
42
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 @input = map { chomp; map { [split /-/] } split /,/ } <>;
  9.  
  10. my $part1 = 0;
  11. my $part2 = 0;
  12.  
  13. foreach my $range (@input) {
  14.     my ($first, $last) = @$range;
  15.  
  16.     foreach my $num ($first .. $last) {
  17.         $part1 += $num  if ($num =~ m#^(.*)\1$#);
  18.         $part2 += $num  if ($num =~ m#^(.*)\1+$#);
  19.     }
  20. }
  21.  
  22. say "Part 1: $part1";
  23. say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment