From bb4cf23d688aa8351ae1efd4a7af429f10822450 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 24 Nov 2022 16:54:01 +0100 Subject: [PATCH] The rest of Year 2017 --- 2017/31.pl | 24 ++++++++++ 2017/32.pl | 47 +++++++++++++++++++ 2017/33.pl | 23 +++++++++ 2017/34.pl | 22 +++++++++ 2017/35.pl | 40 ++++++++++++++++ 2017/36.pl | 63 +++++++++++++++++++++++++ 2017/37.pl | 48 +++++++++++++++++++ 2017/39.pl | 52 +++++++++++++++++++++ 2017/40.pl | 41 ++++++++++++++++ 2017/41.pl | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2017/42.pl | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2017/43.pl | 40 ++++++++++++++++ 2017/44.pl | 43 +++++++++++++++++ 2017/45.pl | 33 +++++++++++++ 2017/46.pl | 16 +++++++ 2017/47.pl | 29 ++++++++++++ 2017/48.pl | 35 ++++++++++++++ 2017/49.pl | 41 ++++++++++++++++ 2017/get.sh | 29 ++++++++++++ 19 files changed, 890 insertions(+) create mode 100755 2017/31.pl create mode 100755 2017/32.pl create mode 100755 2017/33.pl create mode 100755 2017/34.pl create mode 100755 2017/35.pl create mode 100755 2017/36.pl create mode 100755 2017/37.pl create mode 100755 2017/39.pl create mode 100755 2017/40.pl create mode 100755 2017/41.pl create mode 100755 2017/42.pl create mode 100755 2017/43.pl create mode 100755 2017/44.pl create mode 100755 2017/45.pl create mode 100755 2017/46.pl create mode 100755 2017/47.pl create mode 100755 2017/48.pl create mode 100755 2017/49.pl create mode 100755 2017/get.sh diff --git a/2017/31.pl b/2017/31.pl new file mode 100755 index 0000000..89842f6 --- /dev/null +++ b/2017/31.pl @@ -0,0 +1,24 @@ +#!/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; + diff --git a/2017/32.pl b/2017/32.pl new file mode 100755 index 0000000..61d30dd --- /dev/null +++ b/2017/32.pl @@ -0,0 +1,47 @@ +#!/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; + diff --git a/2017/33.pl b/2017/33.pl new file mode 100755 index 0000000..e0ec27a --- /dev/null +++ b/2017/33.pl @@ -0,0 +1,23 @@ +#!/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]; + diff --git a/2017/34.pl b/2017/34.pl new file mode 100755 index 0000000..20aab6d --- /dev/null +++ b/2017/34.pl @@ -0,0 +1,22 @@ +#!/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; + diff --git a/2017/35.pl b/2017/35.pl new file mode 100755 index 0000000..d1a75fc --- /dev/null +++ b/2017/35.pl @@ -0,0 +1,40 @@ +#!/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++; +} + + diff --git a/2017/36.pl b/2017/36.pl new file mode 100755 index 0000000..2f4e59c --- /dev/null +++ b/2017/36.pl @@ -0,0 +1,63 @@ +#!/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; diff --git a/2017/37.pl b/2017/37.pl new file mode 100755 index 0000000..6a21e09 --- /dev/null +++ b/2017/37.pl @@ -0,0 +1,48 @@ +#!/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; + diff --git a/2017/39.pl b/2017/39.pl new file mode 100755 index 0000000..f1ca5b0 --- /dev/null +++ b/2017/39.pl @@ -0,0 +1,52 @@ +#!/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; + } +} + + + + diff --git a/2017/40.pl b/2017/40.pl new file mode 100755 index 0000000..d62cd2a --- /dev/null +++ b/2017/40.pl @@ -0,0 +1,41 @@ +#!/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; + diff --git a/2017/41.pl b/2017/41.pl new file mode 100755 index 0000000..c197c2b --- /dev/null +++ b/2017/41.pl @@ -0,0 +1,132 @@ +#!/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; + + diff --git a/2017/42.pl b/2017/42.pl new file mode 100755 index 0000000..a21fc74 --- /dev/null +++ b/2017/42.pl @@ -0,0 +1,132 @@ +#!/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; + + diff --git a/2017/43.pl b/2017/43.pl new file mode 100755 index 0000000..bc5dceb --- /dev/null +++ b/2017/43.pl @@ -0,0 +1,40 @@ +#!/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; diff --git a/2017/44.pl b/2017/44.pl new file mode 100755 index 0000000..6407fe4 --- /dev/null +++ b/2017/44.pl @@ -0,0 +1,43 @@ +#!/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; diff --git a/2017/45.pl b/2017/45.pl new file mode 100755 index 0000000..b7c7134 --- /dev/null +++ b/2017/45.pl @@ -0,0 +1,33 @@ +#!/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; + + diff --git a/2017/46.pl b/2017/46.pl new file mode 100755 index 0000000..0a8be70 --- /dev/null +++ b/2017/46.pl @@ -0,0 +1,16 @@ +#!/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; diff --git a/2017/47.pl b/2017/47.pl new file mode 100755 index 0000000..5f22a43 --- /dev/null +++ b/2017/47.pl @@ -0,0 +1,29 @@ +#!/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; diff --git a/2017/48.pl b/2017/48.pl new file mode 100755 index 0000000..67d92af --- /dev/null +++ b/2017/48.pl @@ -0,0 +1,35 @@ +#!/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; diff --git a/2017/49.pl b/2017/49.pl new file mode 100755 index 0000000..1bedbe2 --- /dev/null +++ b/2017/49.pl @@ -0,0 +1,41 @@ +#!/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; diff --git a/2017/get.sh b/2017/get.sh new file mode 100755 index 0000000..7b3374a --- /dev/null +++ b/2017/get.sh @@ -0,0 +1,29 @@ +#!/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 -- 2.43.5