You are right about the four squares being merged.
The problem was caused by the original data having an x in an incorrect column. the first x in the second last row
is in the wrong place (off by one). Then I pasted it incorrectly :(
When this is fixed, a 4x4 square results.
#!/usr/bin/perl
# https://perlmonks.org/?node_id=1233341
use strict;
use warnings;
local $_ = do { local $/; <DATA> };
my $original = $_;
s/.\K //g; # tweaks to make it easier to work w
+ith
my $width = /\n/ && $-[0];
s/.+/sprintf "%-${width}s", $&/ge;
my @squares;
my $g = qr/..{$width}/s;
my $letter = 'A';
while( /x| / )
{
my $pos = $-[0];
my $found = $&;
my ($x, $y) = ( $pos % ($width + 1), int $pos / ($width + 1) );
for my $size ( reverse 1 .. $width - 1 )
{
my $sm1 = $size - 1;
pos($_) = $pos;
if( /\G(?=$found {$sm1})(?:$g(?= {$size})){$sm1}/ ) # try magic
{
push @squares, [ $x, $y, $size, $letter ];
for my $n ( 1 .. $size )
{
substr $_, $pos, $size, $letter x $size;
$pos += $width + 1;
}
$letter++;
length $letter > 1 and chop $letter;
last;
}
}
}
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
Which outputs:
[
[1, 1, 3, "A"],
[4, 1, 3, "B"],
[7, 1, 2, "C"],
[9, 1, 1, "D"],
[10, 1, 1, "E"],
[11, 1, 2, "F"],
[9, 2, 1, "G"],
[10, 2, 1, "H"],
[7, 3, 4, "I"],
[11, 3, 2, "J"],
[1, 4, 1, "K"],
[2, 4, 1, "L"],
[3, 4, 2, "M"],
[5, 4, 2, "N"],
[1, 5, 2, "O"],
[11, 5, 2, "P"],
[3, 6, 2, "Q"],
[5, 6, 2, "R"],
[1, 7, 2, "S"],
[7, 7, 1, "T"],
[8, 7, 1, "U"],
[9, 7, 2, "V"],
[11, 7, 2, "W"],
[3, 8, 1, "X"],
[4, 8, 4, "Y"],
[8, 8, 1, "Z"],
[1, 9, 2, "A"],
[3, 9, 1, "B"],
[8, 9, 1, "C"],
[9, 9, 4, "D"],
[3, 10, 1, "E"],
[8, 10, 1, "F"],
[1, 11, 1, "G"],
[2, 11, 2, "H"],
[8, 11, 1, "I"],
[1, 12, 1, "J"],
[4, 12, 1, "K"],
[5, 12, 1, "L"],
[6, 12, 1, "M"],
[7, 12, 1, "N"],
[8, 12, 1, "O"],
]
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
0 1 2 3 4 5 6 7 8 9 0 1 2
1 A A A B B B C C D E F F
2 A A A B B B C C G H F F
3 A A A B B B I I I I J J
4 K L M M N N I I I I J J
5 O O M M N N I I I I P P
6 O O Q Q R R I I I I P P
7 S S Q Q R R T U V V W W
8 S S X Y Y Y Y Z V V W W
9 A A B Y Y Y Y C D D D D
0 A A E Y Y Y Y F D D D D
1 G H H Y Y Y Y I D D D D
2 J H H K L M N O D D D D
|