X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=2022%2F39.pl;h=7f673d79e7f3f0a87a987360dfc87dc66d402708;hb=8bdd97e3fea3291fbc5eea1f20cafb1a7624ab81;hp=b60d35f1efe7363c4325220fe11a6b96e71bb282;hpb=5be58180676de7739990bb3a9d21ac65224e6903;p=aoc.git diff --git a/2022/39.pl b/2022/39.pl index b60d35f..7f673d7 100755 --- a/2022/39.pl +++ b/2022/39.pl @@ -3,27 +3,24 @@ use v5.36; use strict; -chomp(my @list = <>); -my @perm = ( 0 .. $#list ); +my $i = 0; +my @list = map { chomp; [$i++, $_] } <>; 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;