musifter

AoC 2025 day 3 regex version (Perl)

Dec 3rd, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.79 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7.  
  8. # Change this to 2 for part 1
  9. use constant LEN => 12;
  10.  
  11. #
  12. # Mainline
  13. #
  14. my @input = map { chomp; $_ } <>;
  15. my $part2 = 0;
  16.  
  17. # Create regex for finding first digit before a bigger digit
  18. my $regex = '(?:' . join( '|', map { "[1-@{[$_-1]}](?=$_)" } (2 .. 9) ) . ')';
  19.  
  20. foreach my $queue (@input) {
  21.     my @bank = split( //, substr( $queue, 0, -LEN, '' ) );
  22.  
  23.     while (my $jolts = pop @bank) {
  24.         if ($jolts >= substr( $queue, 0, 1 )) {
  25.             $queue = $jolts . $queue;           # add jolts to front
  26.             $queue =~ s#$regex##;               # remove first digit followed by greater
  27.         }
  28.     }
  29.  
  30.     $part2 += substr( $queue, 0, LEN );         # truncate if needed
  31. }
  32.  
  33. say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment