]> www.fi.muni.cz Git - aoc.git/blobdiff - 2017/20.pl
AoC 2017 begins
[aoc.git] / 2017 / 20.pl
diff --git a/2017/20.pl b/2017/20.pl
new file mode 100755 (executable)
index 0000000..dadcfa8
--- /dev/null
@@ -0,0 +1,43 @@
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+chomp(my $line = <>);
+my @l = map { ord } split //, $line;
+push @l, 17, 31, 73, 47, 23;
+
+my $pos = 0;
+my $skip = 0;
+# my @nodes = (0 .. 4);
+my @nodes = (0 .. 255);
+my $n = @nodes;
+
+for (1 .. 64) {
+for my $i (@l) {
+       my $end = $pos + $i;
+       my @to_rev;
+       say "pos=$pos skip $skip i=$i ", join(',', @nodes);
+       if ($end > $n) {
+               push @to_rev, splice @nodes, $pos;
+               push @to_rev, splice @nodes, 0, $end - $n;
+               @to_rev = reverse @to_rev;
+               say "to_rev = ", join(',', @to_rev);
+               unshift @nodes, splice @to_rev, @to_rev-($end-$n);
+               push @nodes, @to_rev;
+       } else {
+               push @to_rev, splice @nodes, $pos, $i;
+               splice @nodes, $pos, 0, reverse @to_rev;
+       }
+       $pos += $i + $skip++;
+       $pos -= $n while $pos >= $n;
+}
+}
+
+my $hash = '';
+while (my @s = splice (@nodes, 0, 16)) {
+       my $x = 0;
+       $x ^= $_ for @s;
+       $hash .= sprintf("%02x", $x);
+}
+say $hash;