Advertisement
musifter

AoC 2022, day 20 (Perl, part 2)

Dec 20th, 2022
1,513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.61 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use List::AllUtils      qw(onlyidx sum);
  7.  
  8. $| = 1;
  9.  
  10. my @list = map { 811589153 * int($_) } <>;
  11. @list = map { [$_, $list[$_]] } (0 .. $#list);
  12.  
  13. foreach (1 .. 10) {
  14.     print ::stderr "Iteration: $_\r";
  15.     for (my $i = 0; $i < @list; $i++) {
  16.         my $idx = onlyidx { $_->[0] == $i } @list;
  17.  
  18.         my $item = splice( @list, $idx, 1 );
  19.         my $dest = ($idx + $item->[1]) % @list;
  20.  
  21.         splice( @list, $dest, 0, $item );
  22.     }
  23. }
  24.  
  25. my $zero = onlyidx { $_->[1] == 0 } @list;
  26. print "\nPart 2: ", sum map { $list[($zero + 1000 * $_) % @list][1] } (1 .. 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement