]> www.fi.muni.cz Git - aoc.git/blob - 2022/39.pl
Day 20: Examine the input carefully!
[aoc.git] / 2022 / 39.pl
1 #!/usr/bin/perl -w
2
3 use v5.36;
4 use strict;
5
6 chomp(my @list = <>);
7 my @perm = ( 0 .. $#list );
8
9 for my $idx (0 .. $#list) {
10         my $move = $list[$idx];
11         my $pos = 0;
12         $pos++ while $perm[$pos] != $idx;
13         splice(@perm, $pos, 1);
14         my $dst = ($pos + $move) % @perm;
15         splice(@perm, $dst, 0, $idx);
16 }
17
18 my @ordered = map { $list[$perm[$_]] } 0 .. $#list;
19
20 my $i = 0;
21 $i++ while $ordered[$i] != 0;
22 my $sum;
23 for (1 .. 3) {
24         $i += 1000;
25         $i %= @ordered;
26         $sum += $ordered[$i];
27 }
28
29 say $sum;