fork download
  1. $\ = $/; ## print behaves as puts
  2.  
  3. <DATA>; # drop 1st input value
  4.  
  5. # take all input lines and split them to characters,
  6. # then append each character to array "verticals" elements:
  7.  
  8. map { chomp; $i = 0; $verticals[ $i ++ ] .= $_ for split // } <DATA>;
  9.  
  10. # construct the line of column values:
  11.  
  12. $line = join '', map {
  13. y/?//d; # delete ?
  14. y///cs; # squeeze
  15. /^.$/ ? $_ : q(?) # if only one char. is left: use it, otherwise mark as ?
  16. } @verticals;
  17.  
  18. ## print $line;
  19.  
  20. ## $_ = "..?X??.X.X.?.?.?.X"; # test line
  21. $_ = $line;
  22.  
  23. # search and substitute until any is possible:
  24.  
  25. nO_Operation while 0
  26. || s/\.\?(?=\.)/.X/g # .?. determine as .X.
  27. || s/X\?(?=X)/X./g
  28. || s/\Q?XX\E/.XX/g # ?XX determine as .XX
  29. || s/\QXX?\E/XX./g
  30. || s/\Q?..\E/X../g
  31. || s/\Q..?\E/..X/g
  32. ;
  33.  
  34. if (/[?]/){print -1} # -1 if there are question mark left
  35. else{
  36. s/(.)\1/1/g, # two consecutive characters convert to 1
  37. s/[^1]/0/g, # any but 1 character convert to 0
  38. }
  39.  
  40. __DATA__
  41. 4
  42. .X??
  43. .??.
  44. ??.?
  45. ?X.?
  46. .X?.
  47.  
Success #stdin #stdout 0s 6000KB
stdin
Standard input is empty
stdout
001