in reply to Tricky math problem ...
Here's one that lets the regex engine find the largest square all by itself :)
#!/usr/bin/perl # https://perlmonks.org/?node_id=1233341 use strict; use warnings; my $original = local $_ = do { local $/; <DATA> }; s/.\K //g; # tweaks to make it easier to work w +ith my $width = /\n/ && $-[0]; s/.+/sprintf "%-${width}s", $&/ge; my @squares; while( s/x| / / ) { my $pos = $-[0]; pos($_) = $pos; /\G( +)(??{ ('.{' . ($width + 1 - length $1) . "}$1") x ~-length $1} +)/s or die "tricky regex failed - oops"; my $size = $& =~ tr/\n// + 1; for my $n ( 1 .. $size ) { substr $_, $pos, $size, ('A' .. 'Z')[@squares % 26] x $size; $pos += $width + 1; } push @squares, [ $pos % ($width + 1), int $pos / ($width + 1), $size + ]; } use Data::Dump 'dd'; dd \@squares; print $original; print s/./$& /gr =~ s/ $//gmr; __DATA__ 0 1 2 3 4 5 6 7 8 9 0 1 2 1 2 x 3 x 4 x 5 x 6 x 7 x x 8 x 9 x x 0 x 1 x x 2
EDIT: removed leftover unneeded instructions.
|
|---|