From 5be58180676de7739990bb3a9d21ac65224e6903 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Tue, 20 Dec 2022 09:33:39 +0100 Subject: [PATCH] Day 20: Examine the input carefully! --- 2022/39.pl | 29 +++++++++++++++++++++++++++++ 2022/40.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100755 2022/39.pl create mode 100755 2022/40.pl 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; -- 2.43.0