From: Jan "Yenya" Kasprzak Date: Tue, 20 Dec 2022 08:33:39 +0000 (+0100) Subject: Day 20: Examine the input carefully! X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=aoc.git;a=commitdiff_plain;h=5be58180676de7739990bb3a9d21ac65224e6903 Day 20: Examine the input carefully! --- diff --git a/2022/39.pl b/2022/39.pl new file mode 100755 index 0000000..b60d35f --- /dev/null +++ b/2022/39.pl @@ -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 index 0000000..b6e9ff4 --- /dev/null +++ b/2022/40.pl @@ -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;