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.


In reply to Re: Tricky math problem ... by tybalt89
in thread Tricky math problem ... by baxy77bax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.