Advertisement
musifter

AoC 2024, day 22 (Perl)

Dec 21st, 2024 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.76 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(max);
  8.  
  9. my @input = map { m#(\d+)#g } <>;
  10.  
  11. my $part1 = 0;
  12. my %hash;
  13.  
  14. foreach my $num (@input) {
  15.     my @window;
  16.     my %tmp;
  17.     my $last = $num % 10;
  18.  
  19.     for (my $i = 0; $i < 2000; $i++) {
  20.         $num = (($num <<  6) ^ $num) & 0xffffff;
  21.         $num = (($num >>  5) ^ $num) & 0xffffff;
  22.         $num = (($num << 11) ^ $num) & 0xffffff;
  23.  
  24.         push( @window, ($num % 10) - $last );
  25.         $last = $num % 10;
  26.  
  27.         if ($i >= 3) {
  28.             $tmp{"@window"} //= $last;
  29.             shift @window;
  30.         }
  31.     }
  32.     $part1 += $num;
  33.     $hash{$_} += $tmp{$_} foreach (keys %tmp);
  34. }
  35.  
  36. say "Part 1: $part1";
  37. say "Part 2: ", max values %hash;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement