Advertisement
musifter

AoC 2024, day 11 (Perl)

Dec 10th, 2024 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.69 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(sum);
  8.  
  9. my %curr = map {$_ => 1} map {m#(\d+)#g} <>;
  10.  
  11. for my $blinks (1 .. 75) {
  12.     my %next;
  13.     foreach my $key (keys %curr) {
  14.         if (length($key) % 2 == 0) {
  15.             my $lhs = int substr( $key, 0, length($key) / 2 );
  16.             my $rhs = int substr( $key,    length($key) / 2 );
  17.             $next{$lhs} += $curr{$key};
  18.             $next{$rhs} += $curr{$key};
  19.         } else {
  20.             $next{$key * 2024 + ($key == 0)} += $curr{$key};
  21.         }
  22.     }
  23.  
  24.     %curr = %next;
  25.     say "Part 1: ", sum values %curr   if ($blinks == 25);
  26. }
  27.  
  28. say "Part 2: ", sum values %curr;
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement