Here is my stab. As kvale notes you can decrease the number of passes.

#!/usr/bin/perl; use strict; use warnings; my @grid = ( [qw(P P B R B)], [qw(P R B B B)], [qw(B R R R B)], [qw(B R R R B)], [qw(B G G B B)], ); $" = ''; my @new; # pass 1 for my $row(0..$#grid) { my $last = $grid[$row][0]; push @{$new[$row*2]}, $grid[$row][0]; for my $pt(0..$#{$grid[$row]}) { next if $pt == 0; if ( $grid[$row][$pt] eq $last ) { push @{$new[$row*2]}, (' ', $grid[$row][$pt]); } else { push @{$new[$row*2]}, ('|', $grid[$row][$pt]); $last = $grid[$row][$pt]; } last if $pt == $#{$grid[$row]}; } last if $row == $#grid; my $width = $#{$grid[$row]}; $new[$row*2+1] = [(' ') x ($width * 2 + 1)]; } # pass 2 for ( my $row=2; $row < @new; $row+=2 ) { for ( my $pt=0; $pt < @{$new[$row]}; $pt +=2 ) { if ( $new[$row][$pt] eq $new[$row-2][$pt] ) { $new[$row-1][$pt] = ' '; } else { $new[$row-1][$pt] = '-'; } } } # find the corners for ( my $row=1; $row < @new; $row+=2 ) { for ( my $pt=1; $pt < @{$new[$row]}; $pt +=2 ) { # do we have corners ? if ( $new[$row][$pt-1].$new[$row-1][$pt] eq '-|' or $new[$row][$pt-1].$new[$row+1][$pt] eq '-|' or $new[$row][$pt+1].$new[$row-1][$pt] eq '-|' or $new[$row][$pt+1].$new[$row+1][$pt] eq '-|' ) { $new[$row][$pt] = '+'; } } } # final pass to complete now we have corners for ( my $row=1; $row < @new; $row+=2 ) { for ( my $pt=1; $pt < @{$new[$row]}; $pt +=2 ) { if ( $new[$row][$pt-1].$new[$row][$pt].$new[$row][$pt+1] eq '- + -' ) { $new[$row][$pt] = '-'; } elsif ( $new[$row-1][$pt].$new[$row][$pt].$new[$row+1][$pt] e +q '| |' ) { $new[$row][$pt] = '|'; } } } print 'Wanted: P P|B|R|B +-+ +-+ P|R|B B B -+ +---+ B|R R R|B | | B|R R R|B +---+-+ B|G G|B B Got: '; print "@$_\n" for @new;

cheers

tachyon


In reply to Re: Re: Map grid to boundary conversion algorithm by tachyon
in thread Map grid to boundary conversion algorithm by Willard B. Trophy

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.