--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my $row = join('', 'a' .. 'p');
+# my $row = join('', 'a' .. 'e');
+
+for my $move (split /,/, <>) {
+ chomp $move;
+ if ($move =~ /s(\d+)/) {
+ $row =~ s/(.*)(.{$1})/\2\1/;
+ } elsif ($move =~ /x(\d+)\/(\d+)/) {
+ my ($a, $b) = (substr($row, $1, 1), substr($row, $2, 1));
+ substr($row, $1, 1) = $b;
+ substr($row, $2, 1) = $a;
+ } elsif ($move =~ /p(\w)\/(\w)/) {
+ eval "\$row =~ y/$1$2/$2$1/";
+ }
+ say $row;
+}
+
+say $row;
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my $row = join('', 'a' .. 'p');
+# my $row = join('', 'a' .. 'e');
+
+chomp (my $dance = <>);
+my @dance = split /,/, $dance;
+
+sub do_dance {
+ for my $move (@dance) {
+ chomp $move;
+ if ($move =~ /s(\d+)/) {
+ $row =~ s/(.*)(.{$1})/\2\1/;
+ } elsif ($move =~ /x(\d+)\/(\d+)/) {
+ my ($a, $b) = (substr($row, $1, 1), substr($row, $2, 1));
+ substr($row, $1, 1) = $b;
+ substr($row, $2, 1) = $a;
+ } elsif ($move =~ /p(\w)\/(\w)/) {
+ eval "\$row =~ y/$1$2/$2$1/";
+ }
+ }
+}
+
+my %seen;
+my $i = 0;
+while (1) {
+ if ($seen{$row}) {
+ last;
+ } else {
+ $seen{$row} = $i;
+ }
+ do_dance();
+ $i++;
+}
+
+my $period = $i - $seen{$row};
+say "period $period from $seen{$row} to $i";
+
+my $count = 1_000_000_000 - $seen{$row};
+$count %= $period;
+do_dance() while $count--;
+
+say $row;
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my $in = 345;
+
+my @buffer = (0);
+my $pos = 0;
+for (1 .. 2017) {
+ # say "$_ $pos ", join(',', @buffer);
+ $pos += $in;
+ if (@buffer > 1) {
+ $pos %= scalar @buffer;
+ } else {
+ $pos = 0;
+ }
+ $pos++;
+ splice @buffer, $pos, 0, $_;
+}
+
+say $buffer[$pos+1];
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my $in = 345;
+
+my $pos = 0;
+my $val;
+for (1 .. 50_000_000) {
+ $pos += $in;
+ if ($_ > 1) {
+ $pos %= $_;
+ } else {
+ $pos = 0;
+ }
+ $pos++;
+ $val = $_ if $pos == 1;
+}
+
+say $val;
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my @code = map { chomp; [ split /\s+/ ] } <>;
+my %regs;
+my $pc = 0;
+my $freq;
+while ($pc < @code) {
+ my ($ins, $a1, $a2) = @{ $code[$pc] };
+ my $v1 = $a1 =~ /[a-z]/ ? $regs{$a1} : $a1
+ if defined $a1;
+ my $v2 = $a2 =~ /[a-z]/ ? $regs{$a2} : $a2
+ if defined $a2;
+ if ($ins eq 'snd') {
+ $freq = $v1;
+ } elsif ($ins eq 'set') {
+ $regs{$a1} = $v2;
+ } elsif ($ins eq 'add') {
+ $regs{$a1} += $v2;
+ } elsif ($ins eq 'mul') {
+ $regs{$a1} *= $v2;
+ } elsif ($ins eq 'mod') {
+ $regs{$a1} %= $v2;
+ } elsif ($ins eq 'rcv') {
+ if ($v1) {
+ $regs{$a1} = $freq;
+ say $freq;
+ exit 0;
+ }
+ } elsif ($ins eq 'jgz') {
+ $pc += $v2-1 if $v1 > 0;
+ } else {
+ die "unknown insn $ins";
+ }
+ $pc++;
+}
+
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my @code = map { chomp; [ split /\s+/ ] } <>;
+
+sub execute {
+ my ($regs, $pc, $qsnd, $qrecv, $nsnd) = @_;
+ while ($$pc < @code) {
+ my ($ins, $a1, $a2) = @{ $code[$$pc] };
+ my $v1 = $a1 =~ /[a-z]/ ? $regs->{$a1} : $a1
+ if defined $a1;
+ my $v2 = $a2 =~ /[a-z]/ ? $regs->{$a2} : $a2
+ if defined $a2;
+ if ($ins eq 'snd') {
+ push @$qsnd, $v1;
+ $$nsnd++ if defined $nsnd;
+ $$pc++;
+ return 0;
+ } elsif ($ins eq 'set') {
+ $regs->{$a1} = $v2;
+ } elsif ($ins eq 'add') {
+ $regs->{$a1} += $v2;
+ } elsif ($ins eq 'mul') {
+ $regs->{$a1} *= $v2;
+ } elsif ($ins eq 'mod') {
+ $regs->{$a1} %= $v2;
+ } elsif ($ins eq 'rcv') {
+ if (@$qrecv) {
+ $regs->{$a1} = shift @$qrecv;
+ } else {
+ return 1;
+ }
+ } elsif ($ins eq 'jgz') {
+ $$pc += $v2-1 if $v1 > 0;
+ } else {
+ die "unknown insn $ins";
+ }
+ $$pc++;
+ }
+ return 2;
+}
+
+my (@q0to1, @q1to0);
+my %regs0 = (p => 0);
+my %regs1 = (p => 1);
+my ($pc0, $pc1) = (0, 0);
+my ($s0, $s1) = (0, 0);
+my $nsnd = 0;
+while (1) {
+ if ($s0 == 0 || ($s0 == 1 && @q1to0)) {
+ $s0 = execute(\%regs0, \$pc0, \@q0to1, \@q1to0);
+ # say "p0 state $s0 at $pc0";
+ }
+ if ($s1 == 0 || ($s1 == 1 && @q0to1)) {
+ $s1 = execute(\%regs1, \$pc1, \@q1to0, \@q0to1, \$nsnd);
+ # say "p1 state $s1 at $pc1";
+ }
+ last if ($s0 >= 1 && $s1 >= 1);
+}
+
+say $nsnd;
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my @map = map { chomp; [ split // ] } <>;
+
+my $xmax = @{ $map[0] };
+my $ymax = @map;
+
+my $x;
+for (0 .. $xmax) {
+ if ($map[0][$_] eq '|') {
+ $x = $_;
+ last;
+ }
+}
+my $y = 0;
+my $dir = 2; # 0 up, 1 right, 2 down, 3 left
+
+my $seen = '';
+while (1) {
+ my $now = $map[$y][$x];
+ say "at $x,$y: '$now'";
+ if ($now =~ /[A-Z]/) {
+ $seen .= $now;
+ } elsif ($now eq '+') {
+ my $ndir;
+ $ndir = 0 if $map[$y-1][$x] ne ' ' && $dir != 2;
+ $ndir = 1 if $map[$y][$x+1] ne ' ' && $dir != 3;
+ $ndir = 2 if $map[$y+1][$x] ne ' ' && $dir != 0;
+ $ndir = 3 if $map[$y][$x-1] ne ' ' && $dir != 1;
+ $dir = $ndir;
+ }
+ if ($dir == 0) {
+ $y--;
+ } elsif ($dir == 1) {
+ $x++;
+ } elsif ($dir == 2) {
+ $y++;
+ } else {
+ $x--;
+ }
+ last if $x < 0 || $y < 0 || $x > $xmax || $y > $ymax || $map[$y][$x] eq ' ';
+}
+
+say $seen;
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my (@acc, @vel, @pos);
+while (<>) {
+ my (@l) = /(-?\d+)/g;
+ push @pos, [ splice(@l, 0, 3) ];
+ push @vel, [ splice(@l, 0, 3) ];
+ push @acc, [ splice(@l, 0, 3) ];
+}
+
+use List::Util qw(sum);
+my ($mina, $minv, $minp, $mini);
+for my $i (0 .. $#acc) {
+ my $a = sum map { abs } @{ $acc[$i] };
+ my $v = sum map { abs } @{ $vel[$i] };
+ my $p = sum map { abs } @{ $pos[$i] };
+ say "$i: $a, $v, $p";
+ if (!defined $mini || $a < $mina) {
+ $mina = $a;
+ $minv = $v;
+ $minp = $p;
+ $mini = $i;
+ } elsif (defined $mini && $a == $mina && $v < $minv) {
+ $minv = $v;
+ $minp = $p;
+ $mini = $i;
+ } elsif (defined $mini && $a == $mina && $v == $minv && $p < $minp) {
+ $minp = $p;
+ $mini = $i;
+ }
+}
+say $mini;
+
+__END__
+while(1) {
+ my (@nvel, @npos);
+ for my $i (0 .. @acc) {
+ my @v;
+ push @v, $vel[$i][$_] + $acc[$i][$_] for 0 .. 2;
+ push @nvel, \@v;
+ my @p;
+ push @p, $pos[$i][$_] + $v[$_] for 0 .. 2;
+ push @npos, \@p;
+ }
+}
+
+
+
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my (@acc, @vel, @pos);
+while (<>) {
+ my (@l) = /(-?\d+)/g;
+ push @pos, [ splice(@l, 0, 3) ];
+ push @vel, [ splice(@l, 0, 3) ];
+ push @acc, [ splice(@l, 0, 3) ];
+}
+
+my %destroyed;
+for (1 .. 1_000) {
+ my (@nvel, @npos);
+ my %pos_now;
+ for my $i (0 .. $#acc) {
+ my @v;
+ push @v, $vel[$i][$_] + $acc[$i][$_] for 0 .. 2;
+ push @nvel, \@v;
+ my @p;
+ push @p, $pos[$i][$_] + $v[$_] for 0 .. 2;
+ push @npos, \@p;
+ if (!$destroyed{$i}) {
+ my $key = join(',', @p);
+ if ($pos_now{$key}) {
+ $destroyed{$pos_now{$key}} = 1;
+ $destroyed{$i} = 1;
+ say "$_ destroyed $i because of $pos_now{$key} at $key";
+ } else {
+ $pos_now{$key} = $i;
+ }
+ }
+ }
+ @vel = @nvel;
+ @pos = @npos;
+}
+
+say $#acc - keys %destroyed;
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my $start = '.#...####';
+
+sub rotate2 {
+ my ($x) = @_;
+ my @x = split //, $x;
+ my $rv = join('', @x[2, 0, 3, 1]);
+ return $rv;
+}
+
+sub rev {
+ my ($str) = @_;
+ my $rlen = length($str) == 4 ? 2 : 3;
+ my (@comps) = $str =~ /(.{$rlen})/g;
+ return join('', map { join('', reverse split(//, $_)) } @comps);
+}
+
+say "rev(.#.#) = ", rev('.#.#');
+
+sub rotate3 {
+ my ($x) = @_;
+ my @x = split //, $x;
+ my $rv = join('', @x[qw(6 3 0 7 4 1 8 5 2)]);
+ return $rv;
+}
+
+sub split4 {
+ my ($x) = @_;
+ my @x = split //, $x;
+ my @rv;
+ for my $str (
+ join('', @x[qw(0 1 4 5)]),
+ join('', @x[qw(2 3 6 7)]),
+ join('', @x[qw(8 9 12 13)]),
+ join('', @x[qw(10 11 14 15)]),
+ ) {
+ push @rv, $str;
+ }
+ return @rv;
+}
+
+sub join6 {
+ my ($s1, $s2, $s3, $s4) = @_;
+ return substr($s1, 0, 3) . substr($s2, 0, 3)
+ . substr($s1, 3, 3) . substr($s2, 3, 3)
+ . substr($s1, 6, 3) . substr($s2, 6, 3)
+ . substr($s3, 0, 3) . substr($s4, 0, 3)
+ . substr($s3, 3, 3) . substr($s4, 3, 3)
+ . substr($s3, 6, 3) . substr($s4, 6, 3)
+}
+
+sub split6 {
+ my ($x) = @_;
+ my @x = split //, $x;
+ return (
+ join('', @x[qw(0 1 6 7)]),
+ join('', @x[qw(2 3 8 9)]),
+ join('', @x[qw(4 5 10 11)]),
+ join('', @x[qw(12 13 18 19)]),
+ join('', @x[qw(14 15 20 21)]),
+ join('', @x[qw(16 17 22 23)]),
+ join('', @x[qw(24 25 30 31)]),
+ join('', @x[qw(26 27 32 33)]),
+ join('', @x[qw(28 29 34 35)]),
+ );
+}
+
+my %rules;
+while (<>) {
+ chomp;
+ my ($src, $dst) = split / => /;
+ $src =~ s/\///g;
+ $dst =~ s/\///g;
+ for (0 .. 3) {
+ $rules{$src} = $dst;
+ my $rev = rev($src);
+ $rules{$rev} = $dst;
+ if (length $src == 4) {
+ $src = rotate2($src);
+ } else {
+ $src = rotate3($src);
+ }
+ }
+}
+
+my %state = ($start => 1);
+my $size = 3;
+for my $step (0 .. 4) {
+ my %nstate;
+ my $part = $step % 3;
+ $size = ($part == 0) ? 4*$size/3 : 3*$size/2;
+ for my $str (keys %state) {
+ if ($part == 0) { # 3 to 4
+ my $nstr = $rules{$str};
+ $nstate{$nstr} += $state{$str};
+ } elsif ($part == 1) { # 4 to 6
+ my @rs;
+ for my $ns (split4($str)) {
+ push @rs, $rules{$ns};
+ }
+ my $nstr = join6(@rs);
+ $nstate{$nstr} += $state{$str};
+ } else { # 6 to 3x3 x 2x2 to 3x3 x 3x3
+ for my $ns (split6($str)) {
+ my $ns1 = $rules{$ns};
+ say "--> $ns => $ns1";
+ $nstate{$ns1} += $state{$str};
+ }
+ }
+ }
+ say "$step/$part $size";
+ for my $str (keys %nstate) {
+ say $str, ' ', length($str), ' ', $nstate{$str};
+ }
+ say "";
+ %state = %nstate;
+}
+
+my $sum;
+for my $str (keys %state) {
+ my $count = () = $str =~ /#/g;
+ say "$str $state{$str} $count";
+ $sum += $state{$str}*$count;
+}
+
+say $sum;
+
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my $start = '.#...####';
+
+sub rotate2 {
+ my ($x) = @_;
+ my @x = split //, $x;
+ my $rv = join('', @x[2, 0, 3, 1]);
+ return $rv;
+}
+
+sub rev {
+ my ($str) = @_;
+ my $rlen = length($str) == 4 ? 2 : 3;
+ my (@comps) = $str =~ /(.{$rlen})/g;
+ return join('', map { join('', reverse split(//, $_)) } @comps);
+}
+
+say "rev(.#.#) = ", rev('.#.#');
+
+sub rotate3 {
+ my ($x) = @_;
+ my @x = split //, $x;
+ my $rv = join('', @x[qw(6 3 0 7 4 1 8 5 2)]);
+ return $rv;
+}
+
+sub split4 {
+ my ($x) = @_;
+ my @x = split //, $x;
+ my @rv;
+ for my $str (
+ join('', @x[qw(0 1 4 5)]),
+ join('', @x[qw(2 3 6 7)]),
+ join('', @x[qw(8 9 12 13)]),
+ join('', @x[qw(10 11 14 15)]),
+ ) {
+ push @rv, $str;
+ }
+ return @rv;
+}
+
+sub join6 {
+ my ($s1, $s2, $s3, $s4) = @_;
+ return substr($s1, 0, 3) . substr($s2, 0, 3)
+ . substr($s1, 3, 3) . substr($s2, 3, 3)
+ . substr($s1, 6, 3) . substr($s2, 6, 3)
+ . substr($s3, 0, 3) . substr($s4, 0, 3)
+ . substr($s3, 3, 3) . substr($s4, 3, 3)
+ . substr($s3, 6, 3) . substr($s4, 6, 3)
+}
+
+sub split6 {
+ my ($x) = @_;
+ my @x = split //, $x;
+ return (
+ join('', @x[qw(0 1 6 7)]),
+ join('', @x[qw(2 3 8 9)]),
+ join('', @x[qw(4 5 10 11)]),
+ join('', @x[qw(12 13 18 19)]),
+ join('', @x[qw(14 15 20 21)]),
+ join('', @x[qw(16 17 22 23)]),
+ join('', @x[qw(24 25 30 31)]),
+ join('', @x[qw(26 27 32 33)]),
+ join('', @x[qw(28 29 34 35)]),
+ );
+}
+
+my %rules;
+while (<>) {
+ chomp;
+ my ($src, $dst) = split / => /;
+ $src =~ s/\///g;
+ $dst =~ s/\///g;
+ for (0 .. 3) {
+ $rules{$src} = $dst;
+ my $rev = rev($src);
+ $rules{$rev} = $dst;
+ if (length $src == 4) {
+ $src = rotate2($src);
+ } else {
+ $src = rotate3($src);
+ }
+ }
+}
+
+my %state = ($start => 1);
+my $size = 3;
+for my $step (0 .. 17) {
+ my %nstate;
+ my $part = $step % 3;
+ $size = ($part == 0) ? 4*$size/3 : 3*$size/2;
+ for my $str (keys %state) {
+ if ($part == 0) { # 3 to 4
+ my $nstr = $rules{$str};
+ $nstate{$nstr} += $state{$str};
+ } elsif ($part == 1) { # 4 to 6
+ my @rs;
+ for my $ns (split4($str)) {
+ push @rs, $rules{$ns};
+ }
+ my $nstr = join6(@rs);
+ $nstate{$nstr} += $state{$str};
+ } else { # 6 to 3x3 x 2x2 to 3x3 x 3x3
+ for my $ns (split6($str)) {
+ my $ns1 = $rules{$ns};
+ say "--> $ns => $ns1";
+ $nstate{$ns1} += $state{$str};
+ }
+ }
+ }
+ say "$step/$part $size";
+ for my $str (keys %nstate) {
+ say $str, ' ', length($str), ' ', $nstate{$str};
+ }
+ say "";
+ %state = %nstate;
+}
+
+my $sum;
+for my $str (keys %state) {
+ my $count = () = $str =~ /#/g;
+ say "$str $state{$str} $count";
+ $sum += $state{$str}*$count;
+}
+
+say $sum;
+
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my %map;
+$; = ',';
+my $y = 0;
+my $px;
+while (<>) {
+ my $x = 0;
+ chomp;
+ for (split //) {
+ $map{$x,$y} = $_ eq '#' ? 1 : 0;
+ $x++;
+ }
+ $px = ($x-1)/2;
+ $y++;
+}
+my $py = ($y-1)/2;
+my $dir = 0; # up right down left
+
+my $sum;
+
+for (1 .. 10_000) {
+ my $state = $map{$px,$py};
+ if ($state) {
+ $dir = 0 if ++$dir > 3;
+ } else {
+ $dir = 3 if --$dir < 0;
+ $sum++;
+ }
+ $map{$px,$py} = !$state;
+ $py-- if $dir == 0;
+ $px++ if $dir == 1;
+ $py++ if $dir == 2;
+ $px-- if $dir == 3;
+}
+
+say $sum;
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my %map;
+$; = ',';
+my $y = 0;
+my $px;
+while (<>) {
+ my $x = 0;
+ chomp;
+ for (split //) {
+ $map{$x,$y} = $_ eq '#' ? 2 : 0;
+ $x++;
+ }
+ $px = ($x-1)/2;
+ $y++;
+}
+my $py = ($y-1)/2;
+my $dir = 0; # up right down left
+
+my $sum;
+
+for (1 .. 10000000) {
+ my $state = $map{$px,$py};
+ if (!$state) {
+ $dir = 3 if --$dir < 0;
+ } elsif ($state == 1) {
+ $sum++;
+ } elsif ($state == 2) {
+ $dir = 0 if ++$dir > 3;
+ } elsif ($state == 3) {
+ $dir ^= 2;
+ }
+ $map{$px,$py} = 0 if ++$map{$px,$py} > 3;
+ $py-- if $dir == 0;
+ $px++ if $dir == 1;
+ $py++ if $dir == 2;
+ $px-- if $dir == 3;
+}
+
+say $sum;
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my @code = map { chomp; [ split /\s+/ ] } <>;
+my %regs;
+my $pc = 0;
+my $muls;
+while ($pc < @code) {
+ my ($ins, $a1, $a2) = @{ $code[$pc] };
+ my $v1 = $a1 =~ /[a-z]/ ? $regs{$a1} : $a1
+ if defined $a1;
+ my $v2 = $a2 =~ /[a-z]/ ? $regs{$a2} : $a2
+ if defined $a2;
+ if ($ins eq 'set') {
+ $regs{$a1} = $v2;
+ } elsif ($ins eq 'sub') {
+ $regs{$a1} -= $v2;
+ } elsif ($ins eq 'mul') {
+ $regs{$a1} *= $v2;
+ $muls++;
+ } elsif ($ins eq 'jnz') {
+ $pc += $v2-1 if $v1;
+ } else {
+ die "unknown insn $ins";
+ }
+ $pc++;
+}
+
+say $muls;
+
+
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+open my $pfh, '<46-primes.txt' or die;
+my %primes = map { chomp; $_ => 1 } <$pfh>;
+my $b = 109300;
+my $c = 126300;
+
+my $h = 0;
+while ($b <= $c) {
+ $h++ if !defined $primes{$b};
+ $b += 17;
+}
+say $h;
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my %neigh;
+while (<>) {
+ my ($src, $dst) = /(\d+)/g;
+ push @{ $neigh{$src} }, $dst;
+ push @{ $neigh{$dst} }, $src;
+}
+
+my %used;
+my $max;
+sub walk {
+ my ($score, @path) = @_;
+ my $now = $path[-1];
+ $max = $score if !$max || $max < $score;
+ say "$score: ", join(' ', @path);
+ for my $next (@{ $neigh{$now} }) {
+ next if $used{$now,$next} || $used{$next,$now};
+ $used{$now,$next} = 1;
+ walk ($score + $now + $next, @path, $next);
+ delete $used{$now,$next};
+ }
+}
+
+walk(0, 0);
+say $max;
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my %neigh;
+while (<>) {
+ my ($src, $dst) = /(\d+)/g;
+ push @{ $neigh{$src} }, $dst;
+ push @{ $neigh{$dst} }, $src;
+}
+
+my %used;
+my $max;
+my $mlen;
+sub walk {
+ my ($score, @path) = @_;
+ my $now = $path[-1];
+ if (!$mlen || $mlen < @path) {
+ $mlen = @path;
+ $max = $score;
+ } elsif ($mlen && $mlen == @path) {
+ $max = $score if $max < $score;
+ }
+ # say "$score: ", join(' ', @path);
+ for my $next (@{ $neigh{$now} }) {
+ next if $used{$now,$next} || $used{$next,$now};
+ $used{$now,$next} = 1;
+ walk ($score + $now + $next, @path, $next);
+ delete $used{$now,$next};
+ }
+}
+
+walk(0, 0);
+say $max;
--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my %st_move;
+my %st_write;
+my %st_next;
+my $state;
+my $val;
+my $start_st;
+my $sum_steps;
+while (<>) {
+ if (/Begin in state (\w+)/) {
+ $start_st = $1;
+ } elsif (/Perform a diagnostic checksum after (\d+) steps/) {
+ $sum_steps = $1;
+ } elsif (/In state (\w+):/) {
+ $state = $1;
+ } elsif (/If the current value is (\d):/) {
+ $val = $1;
+ } elsif (/Write the value (\d)./) {
+ $st_write{$state,$val} = $1;
+ } elsif (/Move one slot to the (\w+)/) {
+ $st_move{$state,$val} = $1 eq 'right' ? 1 : -1;
+ } elsif (/Continue with state (\w+)/) {
+ $st_next{$state,$val} = $1;
+ }
+}
+
+my %tape;
+my $pos = 0;
+while ($sum_steps--) {
+ my $val = $tape{$pos} ? 1 : 0;
+ $tape{$pos} = $st_write{$start_st,$val};
+ $pos += $st_move{$start_st,$val};
+ $start_st = $st_next{$start_st,$val};
+}
+
+use List::Util qw(sum);
+say sum values %tape;
--- /dev/null
+#!/bin/bash
+
+DAY=`date +%d|sed 's/ //g'`
+test -n "$1" && DAY="$1"
+FILE="$((2*DAY - 1))in.txt"
+COOKIE=`cat cookie`
+
+START="6:00:02"
+MAXWAIT=300
+STARTSEC=`date -d "$START" "+%s"`
+NOW=`date "+%s"`
+WAITSEC=`expr $STARTSEC - $NOW`
+
+if [ $WAITSEC -gt 0 -a $WAITSEC -lt $MAXWAIT ]
+then
+ echo "Waiting for $WAITSEC seconds till $START for getting $FILE ..."
+ sleep $WAITSEC
+fi
+
+URL="https://adventofcode.com/2017/day/$DAY/input"
+echo
+echo "Downloading $URL to $FILE"
+curl -s -b "$COOKIE" "$URL" --output "$FILE"
+echo ========================================================================
+cat "$FILE"
+echo ========================================================================
+echo "lines words chars"
+wc "$FILE"
+echo