#!/usr/bin/perl
use strict;
our(@full_square, @empty_square);
my @template = <>; # suck in the puzzle template
chomp @template;

print join "\n", layout_crossword(@template), "\n";


sub layout_crossword {

    my @template = @_;
    my @puzzle;
    foreach (1 .. @template) {
	$puzzle[$_] = ' ' x ($template[$_] * 5 + 1);
    }
    my ($l, $c, $w, $wf);

    foreach my $line (0 .. @template - 1) {
	$c = 0;
	foreach my $char ( 0 .. length ($template[$line]) - 1) {
	    if (substr ($template[$line],$char, 1) eq '.') {
		foreach my $i (0 .. 4) {
		    substr($puzzle[$l * 4 + $i], $c * 5, 6) = $empty_square[$i];
		}
		if ($c == 0 || $l == 0 ||
		    substr($template[$line - 1],$char, 1) ne '.' ||
		    substr($template[$line],$char - 1, 1) ne '.') {
		    $w++;
		    $wf = sprintf("%d",$w);
		    die "too many words in puzzle -- $wf" if length ($wf) > 4;
		    substr($puzzle[$l * 4 + 1], $c * 5 + 1, length ($wf)) = $wf;
		}
	    } else {
		foreach my $i (0 .. 4) {
		    substr($puzzle[$l * 4 + $i], $c * 5, 6) = $full_square[$i];
		}
	    }
	    $c++;
	}
	$l++;
    }
    tr/./ / for @puzzle;
    return @puzzle;
}
