Fun little problem (if I understand it correctly)

#!/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

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, 5, "Y"], [1, 9, 2, "Z"], [3, 9, 1, "A"], [9, 9, 2, "B"], [11, 9, 2, "C"], [3, 10, 1, "D"], [1, 11, 1, "E"], [2, 11, 2, "F"], [9, 11, 2, "G"], [11, 11, 2, "H"], [1, 12, 1, "I"], ] 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 Y V V W W 9 Z Z A Y Y Y Y Y B B C C 0 Z Z D Y Y Y Y Y B B C C 1 E F F Y Y Y Y Y G G H H 2 I F F Y Y Y Y Y G G H H

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.