sub layout_crossword {
    my (@puzzle) = @_;

    #make local copy of the globals
    my @empty_sq = @empty_square;
    my @full_sq = @full_square;

    my $sq_height = scalar @empty_square;
    my $sq_len = length $empty_square[0];

    warn "Empty and Full squares are not square\n" if (scalar @full_square) != $sq_height;
    warn "Empty and Full squares are not square\n" if (length $full_square[0]) != $sq_len;

    #ignore bottom line of squares
    $sq_height--;

    #rsult array
    my @result;
    
    #remove last char of each line in quares
    my $lastchar = chop(@empty_sq);
    chop(@full_sq);

    #use this to "close" the box
    my $horzline = $full_sq[0];
    
    #use overhang to track if current line overhangs
    my $overhang = 0;

    my @squares;
    my $y=0;

    #next two vars used in numbering the puzzle
    my $current_number=1;
    my @prev_line;

    my $len=0;

    #iterate over the lines in the passed puzzle
    foreach my $line(@puzzle) {
        
        @squares = split(//,$line);

        #allows irregular shaped puzzles
        $overhang = scalar(@prev_line) - scalar(@squares);
        $overhang = 0 if $overhang<0;

        $len = length $line;

        #next_line is needed to check for numbering
        my @next_line = split(//,$puzzle[$y+1]) if defined $puzzle[$y+1];

        #iterate over the lines in the empty/full_square
        for (my $idx=0;$idx<$sq_height; $idx++) {
            my $current_line="";   #this gets pushed into the @result array

            my $templen=$len;
            
            if (($overhang>0) && ($idx == 0)) {
                 $templen += $overhang;
            }
            
            #iterate over the @squares array
            for (my $x=0;$x<$templen;$x++) {

                my $temp_str = "";

                if ($squares[$x] eq ".") {   #empty square
                    
                    $temp_str = $empty_sq[$idx];
                    
                    #check if we need to number it.
                    if (($idx==1) &&
                        ( (($x==0)&& ($squares[$x+1] eq ".")) ||
                        (($y==0) && ($next_line[$x] eq ".")) ||
                        (($squares[$x-1] ne ".") && ($squares[$x+1] eq ".")) ||
                        (($prev_line[$x] ne ".") && ($next_line[$x] eq ".")) ))
                    {
                        my $number = sprintf("%d",$current_number++);
                    
                        $number .= " " while (length $number<$sq_len-2);

                        die "Number doesnt fit in square\n" if length($number) > $sq_len-2;
                        $temp_str = $lastchar . $number;
                    }
                }
                else { #full square
                    $temp_str = $full_sq[$idx];
                }
                $current_line .= $temp_str;
            }
            $current_line .= $lastchar;
            #replace all dots with spaces.
            $current_line =~ s/\./ /g;

            push(@result, $current_line);
        }
        @prev_line = @squares;
        $y++;
    }
    
    #add the final horizontal line.
    push(@result, $horzline x $len . $lastchar);

    return @result;
}

 

