]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 20: Examine the input carefully!
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 20 Dec 2022 08:33:39 +0000 (09:33 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 20 Dec 2022 08:33:39 +0000 (09:33 +0100)
2022/39.pl [new file with mode: 0755]
2022/40.pl [new file with mode: 0755]

diff --git a/2022/39.pl b/2022/39.pl
new file mode 100755 (executable)
index 0000000..b60d35f
--- /dev/null
@@ -0,0 +1,29 @@
+#!/usr/bin/perl -w
+
+use v5.36;
+use strict;
+
+chomp(my @list = <>);
+my @perm = ( 0 .. $#list );
+
+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 @ordered = map { $list[$perm[$_]] } 0 .. $#list;
+
+my $i = 0;
+$i++ while $ordered[$i] != 0;
+my $sum;
+for (1 .. 3) {
+       $i += 1000;
+       $i %= @ordered;
+       $sum += $ordered[$i];
+}
+
+say $sum;
diff --git a/2022/40.pl b/2022/40.pl
new file mode 100755 (executable)
index 0000000..b6e9ff4
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/perl -w
+
+use v5.36;
+use strict;
+
+my $key = 811589153;
+my @list = map { $key * $_ } <>;
+my @perm = ( 0 .. $#list );
+
+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 @ordered = map { $list[$perm[$_]] } 0 .. $#list;
+
+my $i = 0;
+$i++ while $ordered[$i] != 0;
+
+my $sum;
+for (1 .. 3) {
+       $i += 1000;
+       $i %= @ordered;
+       $sum += $ordered[$i];
+}
+
+say $sum;