X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;ds=sidebyside;f=2022%2F40.pl;h=5451455a5ea0d9105c5e7b3697f79ed6ba48177d;hb=8bdd97e3fea3291fbc5eea1f20cafb1a7624ab81;hp=b6e9ff429e77485cdecc5346f277930fbbec0697;hpb=5be58180676de7739990bb3a9d21ac65224e6903;p=aoc.git diff --git a/2022/40.pl b/2022/40.pl index b6e9ff4..5451455 100755 --- a/2022/40.pl +++ b/2022/40.pl @@ -4,30 +4,26 @@ use v5.36; use strict; my $key = 811589153; -my @list = map { $key * $_ } <>; -my @perm = ( 0 .. $#list ); +my $i = 0; +my @list = map { [$i++, $key * $_] } <>; for (1 .. 10) { for my $idx (0 .. $#list) { - my $move = $list[$idx]; - my $pos = 0; - $pos++ while $perm[$pos] != $idx; - splice(@perm, $pos, 1); - my $dst = ($pos + $move) % @perm; - splice(@perm, $dst, 0, $idx); + my $i = 0; + $i++ while $list[$i][0] != $idx; + my $item = splice @list, $i, 1; + my $dest = ($item->[1] + $i) % @list; + splice @list, $dest, 0, $item; } } -my @ordered = map { $list[$perm[$_]] } 0 .. $#list; - -my $i = 0; -$i++ while $ordered[$i] != 0; - +$i = 0; +$i++ while $list[$i][1] != 0; my $sum; for (1 .. 3) { $i += 1000; - $i %= @ordered; - $sum += $ordered[$i]; + $i %= @list; + $sum += $list[$i][1]; } say $sum;