musifter

AoC 2025 day 3 list version (Perl)

Dec 3rd, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.78 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. sub chain_index (&@) {
  12.     my $block = shift;
  13.     my $ret = -1;
  14.     foreach my $idx (0 .. $#_ - 1) {
  15.         if (&$block( $_[$idx], $_[$idx+1] )) {
  16.             $ret = $idx;
  17.             last;
  18.         }
  19.     }
  20.     return ($ret);
  21. }
  22.  
  23. #
  24. # Mainline
  25. #
  26. my @input = map { chomp; [split //] } <>;
  27. my $part2 = 0;
  28.  
  29. foreach my $bank (@input) {
  30.     my @queue = splice( @$bank, -LEN );
  31.  
  32.     while (my $jolts = pop @$bank) {
  33.         if ($jolts >= $queue[0]) {
  34.             splice( @queue, (chain_index {$_[0] < $_[1]} @queue), 1 );
  35.             unshift( @queue, $jolts );
  36.         }
  37.     }
  38.  
  39.     $part2 += join( '', @queue );
  40. }
  41.  
  42. say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment