Count the crossing words
Slip, 18 + 3 = 21 bytes
>? ( +(X|$^)<<){2}
Run with the flags no
(hence the +3), and uses space/X
instead of space/#
. Annoyingly this is longer than CJam/Pyth, but I guess Slip wasn't designed to be particular golfy...
Try it online. Note that the first example is missing spaces on a few lines.
Explanation
>? Optionally turn right, hence matching either horizontally or vertically
[space] Match a space
( ){2} Group, twice:
[space]+ Match 1+ spaces
(X|$^) Either an X or the boundary of the grid
<< Reverse the match pointer by turning left twice
The n
flag makes output print the number of matches, and the o
flag enables overlapping matches starting from the same square. The reason for the back-and-forth is because Slip tries matches starting from every square, and we want to make sure we only match a full row rather than a partial one. Slip only returns unique matches, even if they started from different positions.
Note: Originally I had >?( +(X|$^)<<){2}
, with the first space on the inside. This would miss some cases with 2 space long words at the edge, since the pointer would go like this:
XXX XXX XXX XXX
X> X > X< <
XXX XXX XXX XXX
[sp] [sp]+$^ <<[sp] [sp]+ (uh oh match fails)
CJam, 18 17 13 11 bytes
2 bytes saved by Dennis.
Uses spaces for filled cells and 1
for empty cells:
qN/_z+:~1-,
Test it here.
Explanation
q e# Read the entire input.
N/ e# Split into lines.
_z e# Make a copy and transpose it.
+ e# Add the lines of the two grids together.
:~ e# Evaluate each line which will push a rep-digit number for each empty-cell chunk.
1- e# Remove all the 1s as these correspond to individual empty cells.
, e# Get the length of the array.
Haskell, 81 bytes
import Data.List
m x=sum[1|(_:_:_)<-words x]
f x=m x+m(unlines$transpose$lines x)
Uses spaces as block characters and any other (non whitespace) character as an empty cell.
How it works: split input into list of words at spaces. Take a 1
for every word with at lease 2 characters and sum those 1
s. Apply the same procedure to the transposition (split at \n
) of the input. Add both results.