My third word search related challenge in a row. :)
Challenge:
Brief explanation of what a word search is:
In a word search you'll be given a grid of letters and a list of words. The idea is to cross off the words from the list in the grid. The words can be in eight different directions: horizontally from left-to-right or right-to-left; vertically from top-to-bottom or bottom-to-top; diagonally from the topleft-to-bottomright or bottomright-to-topleft; or anti-diagonally from the topright-to-bottomleft or bottomleft-to-topright.
Actual challenge:
Given a list of coordinates indicating the crossed out words within a grid (in any reasonable format†), output how many line-islands there are.
We've had challenges involving finding the amount of islands in a matrix. But in this case it's different: just looking at the grid, words could be right next to each other, but would still be two separated line-islands. E.g. In the following partially solved word search, we have four separated line-islands, even though the letters of the words are right next to each other.
This partially solved word search above would result in an output of 4
.
If 'word' FG
was FGH
like this:
The output would have been 3
instead, because the FGH
+NKH
now form a single connected line-island.
In this challenge we'll only be taking the coordinates of the words in a grid as input, and output the amount of line-islands.
Challenge rules:
- † You're allowed to take the input in any reasonable format. It could be pair of coordinates per word to indicate their start/end positions (e.g.
[[[0,0],[0,2]],[[1,1],[1,3]],[[3,1],[1,3]],[[3,2],[2,3]]]
for the second example above); it could be a full list of coordinates per word (e.g.[[[0,0],[0,1],[0,2]],[[1,1],[1,2],[1,3]],[[3,1],[2,2],[1,3]],[[3,2],[2,3]]]
); a list of bit-mask matrices per word (e.g.[[[1,1,1,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,1,1,1],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,1],[0,0,1,0],[0,1,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,1],[0,0,1,0]]]
); etc. If you're unsure if a possible input-format is valid, leave a comment down below. - All 'words' are guaranteed to have at least two letters.
- You can assume the input won't be empty.
- You can optionally take the dimensions of the grid as additional input.
- Note that coordinates of 'words' are not guaranteed to be in the same positions to still form a single line-island! E.g. this grid would result in
1
because the lines cross, but neither 'word' share the same letter-positions:
General rules:
- This is code-golf, so the shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (e.g. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
- Input as start/ending coordinates of the 'words':
[[[0,0],[0,2]],[[1,1],[1,2]],[[3,1],[1,3]],[[3,2],[2,3]]]
- Input as coordinates of the complete 'words':
[[[0,0],[0,1],[0,2]],[[1,1],[1,2]],[[3,1],[2,2],[1,3]],[[3,2],[2,3]]]
- Input as bit-mask matrices of the 'words':
[[[1,1,1,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,1,1,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,1],[0,0,1,0],[0,1,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,1],[0,0,1,0]]]
Output: 4
- Input as start/ending coordinates of the 'words':
[[[0,0],[0,2]],[[1,1],[1,3]],[[3,1],[1,3]],[[3,2],[2,3]]]
- Input as coordinates of the complete 'words':
[[[0,0],[0,1],[0,2]],[[1,1],[1,2],[1,3]],[[3,1],[2,2],[1,3]],[[3,2],[2,3]]]
- Input as bit-mask matrices of the 'words':
[[[1,1,1,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,1,1,1],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,1],[0,0,1,0],[0,1,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,1],[0,0,1,0]]]
Output: 3
- Input as start/ending coordinates of the 'words':
[[[0,0],[1,1]],[[0,1],[1,0]]]
- Input as coordinates of the complete 'words':
[[[0,0],[1,1]],[[0,1],[1,0]]]
- Input as bit-mask matrices of the 'words':
[[[1,0],[0,1]],[[0,1],[1,0]]]
Output: 1
- Input as start/ending coordinates of the 'words':
[[[1,9],[8,9]],[[8,6],[1,6]],[[1,4],[4,7]],[[9,0],[0,9]],[[1,0],[6,0]],[[6,1],[3,4]],[[9,4],[9,9]],[[0,1],[0,8]],[[9,3],[1,3]],[[0,0],[9,9]]]
- Input as coordinates of the complete 'words':
[[[1,9],[2,9],[3,9],[4,9],[5,9],[6,9],[7,9],[8,9]],[[8,6],[7,6],[6,6],[5,6],[4,6],[3,6],[2,6],[1,6]],[[1,4],[2,5],[3,6],[4,7]],[[9,0],[8,1],[7,2],[6,3],[5,4],[4,5],[3,6],[2,7],[1,8],[0,9]],[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0]],[[6,1],[5,2],[4,3],[3,4]],[[9,4],[9,5],[9,6],[9,7],[9,8],[9,9]],[[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8]],[[9,3],[8,3],[7,3],[6,3],[5,3],[4,3],[3,3],[2,3],[1,3]],[[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]]]
- Input as bit-mask matrices of the 'words': pastebin
Output: 4
- Input as start/ending coordinates of the 'words':
[[[8,6],[1,6]],[[1,4],[4,7]],[[9,0],[0,9]],[[6,1],[3,4]],[[9,4],[9,9]],[[9,3],[1,3]],[[0,0],[9,9]]]
- Input as coordinates of the complete 'words':
[[[8,6],[7,6],[6,6],[5,6],[4,6],[3,6],[2,6],[1,6]],[[1,4],[2,5],[3,6],[4,7]],[[9,0],[8,1],[7,2],[6,3],[5,4],[4,5],[3,6],[2,7],[1,8],[0,9]],[[6,1],[5,2],[4,3],[3,4]],[[9,4],[9,5],[9,6],[9,7],[9,8],[9,9]],[[9,3],[8,3],[7,3],[6,3],[5,3],[4,3],[3,3],[2,3],[1,3]],[[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]]]
- Input as bit-mask matrices of the 'words': pastebin
Output: 1