#!/usr/bin/perl
#
# Perl QOTW #5
# entry by Shawn Poulson, 11/14/2002
# shawn.poulson@sap.com
#

my @sample_puzzle = qw(
		       ....X.....X....
		       ....X.....X....
		       ....X.....X....
		       .......X.......
		       XXX......X.....
		       .....X......XXX
		       ......X...X....
		       ...X.......X...
		       ....X...X......
		       XXX......X.....
		       .....X......XXX
		       .......X.......
		       ....X.....X....
		       ....X.....X....
		       ....X.....X....
		       );

my @rv = layout_crossword(@sample_puzzle);
print join("\n",@rv) . "\n";
exit;

sub layout_crossword {
   my @puzzle = @_;
   my @result = ();
   my $counter = 1;
   my $rowabove = 'X' x length($puzzle[0]);
   for my $rownum (0..$#puzzle) {
      my @rowresult = ();
      my $left = 'X';
      my $row = $puzzle[$rownum];
      for my $colnum (0..length($row)-1) {
	 my $current = substr($row,$colnum,1);
	 my $above = substr($rowabove,$colnum,1);
	 my @square = ($current eq 'X') ? @full_square : @empty_square;
	 substr($square[1],1,4) = sprintf("%-4d", $counter++) if ($current ne 'X' && ($left eq 'X' || $above eq 'X'));
	 for (0..$#square) {
	    my $t = $square[$_];
	    $t =~ s/\./ /g;
            $t =~ s/.// if ($colnum > 0);
	    $rowresult[$_] .= $t;
	    $left = $current;
	 }
      }
      splice(@rowresult,0,1) unless ($rownum == 0);
      push(@result,@rowresult);
      $rowabove = $row;
   }
   return(@result);
}


