$\ = $/; ## print behaves as puts
<DATA>; # drop 1st input value
# take all input lines and split them to characters,
# then append each character to array "verticals" elements:
map { chomp; $i = 0; $verticals[ $i ++ ] .= $_ for split // } <DATA>;
# construct the line of column values:
y/?//d; # delete ?
/^.$/ ? $_ : q(?) # if only one char. is left: use it, otherwise mark as ? } @verticals;
## print $line;
## $_ = "..?X??.X.X.?.?.?.X"; # test line
$_ = $line;
# search and substitute until any is possible:
nO_Operation while 0
|| s/\.\?(?=\.)/.X/g # .?. determine as .X.
|| s/X\?(?=X)/X./g
|| s/\Q?XX\E/.XX/g # ?XX determine as .XX
|| s/\QXX?\E/XX./g
|| s/\Q?..\E/X../g
|| s/\Q..?\E/..X/g
;
if (/[?]/){print -1} # -1 if there are question mark left else{
s/(.)\
1/1/g
, # two consecutive characters convert to 1 s/[^1]/0/g
, # any but 1 character convert to 0 }
__DATA__
4
.X??
.??.
??.?
?X.?
.X?.
JFwgPSAkLzsgIyMgcHJpbnQgYmVoYXZlcyBhcyBwdXRzCgo8REFUQT47CSMgZHJvcCAxc3QgaW5wdXQgdmFsdWUKCiMgdGFrZSBhbGwgaW5wdXQgbGluZXMgYW5kIHNwbGl0IHRoZW0gdG8gY2hhcmFjdGVycywgCiMgdGhlbiBhcHBlbmQgZWFjaCBjaGFyYWN0ZXIgdG8gYXJyYXkgInZlcnRpY2FscyIgZWxlbWVudHM6CgptYXAgeyBjaG9tcDsgJGkgPSAwOyAkdmVydGljYWxzWyAkaSArKyBdIC49ICRfIGZvciBzcGxpdCAvLyB9IDxEQVRBPjsKCiMgY29uc3RydWN0IHRoZSBsaW5lIG9mIGNvbHVtbiB2YWx1ZXM6CgokbGluZSA9IGpvaW4gJycsIG1hcCB7Cgl5Lz8vL2Q7IAkJCSMgZGVsZXRlID8KCXkvLy9jczsgCQkJIyBzcXVlZXplCgkvXi4kLyA/ICRfIDogcSg/KQkJIyBpZiBvbmx5IG9uZSBjaGFyLiBpcyBsZWZ0OiB1c2UgaXQsIG90aGVyd2lz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