--- /dev/null
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my @m = split //, <>;
+scalar <>;
+
+$; = ',';
+my @image = map { chomp; [ split // ] } <>;
+
+my $odd = 1;
+sub enhance {
+ my $img = shift;
+ my $ymax = @$img;
+ my $xmax = @{ $img->[0] };
+
+ say "odd=$odd, m0=$m[0]";
+
+ my @rv;
+ for my $y (0 .. $ymax+3) {
+ for my $x (0 .. $xmax+3) {
+ my $idx = 0;
+ for my $dy (-2 .. 0) {
+ my $y2 = $y+$dy;
+ for my $dx (-2 .. 0) {
+ my $x2 = $x+$dx;
+ # say "$x,$y + $dx,$dy = $x2, $y2";
+ $idx <<= 1;
+ if ($y2 >= 0 && $y2 < $ymax
+ && $x2 >= 0 && $x2 < $xmax) {
+ $idx++ if $img->[$y2][$x2] eq '#';
+ } else {
+ $idx++ if !$odd && ($m[0] eq '#');
+ }
+ }
+ }
+ # say "$x,$y => $idx => $m[$idx]";
+ $rv[$y][$x] = $m[$idx];
+ }
+ }
+
+ $odd = !$odd;
+
+ return @rv;
+}
+
+sub imgdump {
+ my $img = shift;
+ my $ymax = @$img;
+ my $xmax = @{ $img->[0] };
+
+ my $sum;
+
+ say "$xmax x $ymax";
+ for my $y (0 .. $ymax-1) {
+ for my $x (0 .. $xmax-1) {
+ print $img->[$y][$x];
+ $sum++ if $img->[$y][$x] eq '#';
+ }
+ print "\n";
+ }
+ say "sum=$sum";
+}
+
+for (1 .. 50) {
+ imgdump(\@image);
+ @image = enhance(\@image);
+}
+imgdump(\@image);
+my $sum = 0;
+for my $row (@image) {
+ for my $pixel (@$row) {
+ $sum++ if $pixel eq '#';
+ }
+}
+say "pixels = $sum";