]> www.fi.muni.cz Git - aoc.git/blob - 2017/27.pl
Day 25: examining the input
[aoc.git] / 2017 / 27.pl
1 #!/usr/bin/perl
2
3 use v5.30;
4 use strict;
5
6 my $in = 'stpzcrnm';
7 # my $in = 'flqrgnkx';
8
9 sub knot {
10         my $line = shift;
11         my @l = map { ord } split //, $line;
12         push @l, 17, 31, 73, 47, 23;
13
14         my $pos = 0;
15         my $skip = 0;
16         # my @nodes = (0 .. 4);
17         my @nodes = (0 .. 255);
18         my $n = @nodes;
19
20         for (1 .. 64) {
21                 for my $i (@l) {
22                         my $end = $pos + $i;
23                         my @to_rev;
24                         # say "pos=$pos skip $skip i=$i ", join(',', @nodes);
25                         if ($end > $n) {
26                                 push @to_rev, splice @nodes, $pos;
27                                 push @to_rev, splice @nodes, 0, $end - $n;
28                                 @to_rev = reverse @to_rev;
29                                 # say "to_rev = ", join(',', @to_rev);
30                                 unshift @nodes, splice @to_rev, @to_rev-($end-$n);
31                                 push @nodes, @to_rev;
32                         } else {
33                                 push @to_rev, splice @nodes, $pos, $i;
34                                 splice @nodes, $pos, 0, reverse @to_rev;
35                         }
36                         $pos += $i + $skip++;
37                         $pos -= $n while $pos >= $n;
38                 }
39         }
40
41         my $hash = '';
42         while (my @s = splice (@nodes, 0, 16)) {
43                 my $x = 0;
44                 $x ^= $_ for @s;
45                 $hash .= sprintf("%08b", $x);
46         }
47         return $hash;
48 }
49
50 my $used = 0;
51 for my $row (0 .. 127) {
52         my $h = knot("$in-$row");
53         $used++ for $h =~ /1/g;
54 }
55 say $used;