musifter

AoC 2025, day 3 (Perl)

Dec 2nd, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.64 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7. use List::Util      qw(reduce);
  8.  
  9. use constant LEN => 12;
  10.  
  11. my @input = map { chomp; [split //] } <>;
  12. my $part2 = 0;
  13.  
  14. foreach my $bank (@input) {
  15.     my @queue = splice( @$bank, -LEN );
  16.  
  17.     BATTERY:
  18.     for (my $i = $bank->$#*; $i >= 0; $i--) {
  19.         my $jolts = $bank->[$i];
  20.  
  21.         # Cascade jolts down the queue:
  22.         for (my $j = 0; $j < LEN; $j++) {
  23.             next BATTERY  if ($jolts < $queue[$j]);
  24.             ($queue[$j], $jolts) = ($jolts, $queue[$j]);
  25.         }
  26.     }
  27.  
  28.     $part2 += reduce { 10 * $a + $b } @queue;
  29. }
  30.  
  31. say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment