Introduction:
In my recent Strikethrough the Word Search List challenge I mentioned the following:
When I do the word-search puzzles, I (almost) always go over the words in order, and strike them through one by one.
In some cases I do them in reversed order as a change of pace, although that doesn't matter too much right now. When I do them in the correct alphabetical order however, I sometimes already see the word after the one I'm currently searching for. In those cases, I usually 'sneakily' strike through that next word already, before I continue searching for the actual current word.
In almost™ all cases, this occurs when both words have the same starting letter, and the next word that I find accidentally is in a horizontal left-to-right direction.
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.
Challenge:
Today's challenge is simple. Given a grid of letters and a list of words, output the maximum amount of times what I describe above can occur.
We do this with two steps:
- Find all words from the given list which can be found in the grid in a horizontal left-to-right direction.
- For each of those words, check if the word before it in the given list starts with the same letter.
Example:
Grid:
JLIBPNZQOAJD
KBFAMZSBEARO
OAKTMICECTQG
YLLSHOEDAOGU
SLHCOWZBTYAH
MHANDSAOISLA
TOPIFYPYAGJT
EZTBELTEATAZ
Words:
BALL
BAT
BEAR
BELT
BOY
CAT
COW
DOG
GAL
HAND
HAT
MICE
SHOE
TOP
TOYS
ZAP
Horizontal left-to-right words:
Word-pairs of these horizontal left-to-right words, with its preceding word in the list:
Words:
BAT,BEAR ← B
BEAR,BELT ← B
CAT,COW ← C
GAL,HAND
HAT,MICE
MICE,SHOE
SHOE,TOP
From these pairs, three start with the same letters, so the output is 3
.
Challenge rules:
- As you may have noted above, we only look at the word directly preceding it. For the
BELT
in the example,BALL
,BAT
,BEAR
are all three before it and start with aB
as well, but we only look at the word directly preceding it (BEAR
in this case), and the counter would only increase by 1 for the output. - If the very first word in the list is a horizontal left-to-right word, there is obviously no word before it.
- The list of words is guaranteed to contain at least two words, and all words are guaranteed to be present in the given grid.
- You can take the inputs in any reasonable format. Could be from STDIN input-lines; as a list of lines; a matrix of characters; etc.
- You can optionally take the dimensions of the grid as additional input.
- All words are guaranteed to have at least two letters.
- You can assume each word is only once in the grid.
- You can assume the list of words are always in alphabetical order.
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:
Inputs:
JLIBPNZQOAJD
KBFAMZSBEARO
OAKTMICECTQG
YLLSHOEDAOGU
SLHCOWZBTYAH
MHANDSAOISLA
TOPIFYPYAGJT
EZTBELTEATAZ
BALL
BAT
BEAR
BELT
BOY
CAT
COW
DOG
GAL
HAND
HAT
MICE
SHOE
TOP
TOYS
ZAP
Output: 3
Inputs:
ABC
SRO
KAX
AB
ASK
ARB
ARX
AX
Output: 1
Inputs:
WVERTICALL
ROOAFFLSAB
ACRILIATOA
NDODKONWDC
DRKESOODDK
OEEPZEGLIW
MSIIHOAERA
ALRKRRIRER
KODIDEDRCD
HELWSLEUTH
BACKWARD
DIAGONAL
FIND
HORIZONTAL
RANDOM
SEEK
SLEUTH
VERTICAL
WIKIPEDIA
WORDSEARCH
Output: 1
AYCD
EFGH
DCBA
ABC
AYC
CB
CBA
CD
EF
EFGH
Output: 4