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