]> www.fi.muni.cz Git - aoc.git/blobdiff - 2017/27.pl
AoC 2017 days 11 to 15
[aoc.git] / 2017 / 27.pl
diff --git a/2017/27.pl b/2017/27.pl
new file mode 100755 (executable)
index 0000000..dd0a244
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my $in = 'stpzcrnm';
+# my $in = 'flqrgnkx';
+
+sub knot {
+       my $line = shift;
+       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("%08b", $x);
+       }
+       return $hash;
+}
+
+my $used = 0;
+for my $row (0 .. 127) {
+       my $h = knot("$in-$row");
+       $used++ for $h =~ /1/g;
+}
+say $used;