I opted for the "abuse regex" approach (because it's fun, and you can do it incrementally one s/// at a time until it works):
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1121552
use strict;
use warnings;
for my $y ( 1..25 )
{
local $_ = tile(5,$y);
/1$/ and !/-/ and print "success 5,$y\n";
}
sub tile
{
my ($w, $h) = @_;
my $wm1 = $w - 1;
local $_ = ( '-' x $w . "\n" ) x $h;
s/-/1/;
1 while # print("$_\n"),
s/1..{$w}\K-/1/s ||
s/1\n.*\K-(?=\n)/2/ ||
s/1(?=.*$)\K-/4/ ||
s/2.{$wm1}\K-/2/s ||
s/2.{$wm1}\n\K-/1/ ||
s/-(?=2.*$)/3/ ||
s/-(?=..{$w}3)/3/s ||
s/^.*\K-(?=3)/2/ ||
s/-(?=.{$wm1}\n3)/4/ ||
s/-(?=.{$wm1}4)/4/s ||
s/^.*4\K-/1/ ||
s/-(?=.{$w}4\n)/3/s;
return $_;
}
which prints
success 5,1
success 5,3
success 5,7
success 5,9
success 5,11
success 5,13
success 5,17
success 5,19
success 5,21
success 5,23
Sure looks like multiple solutions exist.
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|