]> www.fi.muni.cz Git - aoc.git/blobdiff - 2022/39.pl
Day 20: alternative solution wit list of pairs - slower but prettier
[aoc.git] / 2022 / 39.pl
index b60d35f1efe7363c4325220fe11a6b96e71bb282..7f673d79e7f3f0a87a987360dfc87dc66d402708 100755 (executable)
@@ -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;