Introduction:
Apparently I keep coming up with word search related challenges lately. :)
When I do the word search in the Dutch news paper, some words are very easy to find because they contain letters that aren't too common in Dutch words, like x
or q
. So although I usually look for the first letter or prefix of a word I'm searching, in some cases looking for these letters in the grid is faster to find the words.
Brief explanation of what a word search is†:
† Although it's not too relevant for the actual challenge this time.
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:
Given a grid of letters and a list of words, output for each word the lowest count of the letters within this word within the grid.
For example:
Grid:
REKNA
TAXIJ
RAREN
ATAEI
YCYAN
Words:
AIR
ANKER
EAT
CYAN
NINJA
RARE
TAXI
TRAY
XRAY
YEN
For AIR
we see the following frequency of the letters in the grid: [A:6, I:2, R:3]
, of which the lowest is I:2
. Doing something similar for the other words, the result would be AIR:2, ANKER:1, EAT:2, CYAN:1, NINJA:1, RARE:3, TAXI:1, TRAY:2, XRAY:1, YEN:2
.
Challenge rules:
- You can take the inputs in any reasonable format. Could be from STDIN input-lines; as a list of lines; a matrix of characters; as codepoint-integers; etc.
- You can optionally take the dimensions of the grid as additional input.
- The output can be in any reasonable format as well. Can be a key-value map of word + integer as above, but can also just be a list of the integers (e.g.
[2,1,2,1,1,3,1,2,1,2]
for the example above. - You can assume the list of words are always in alphabetical order.
- The list of words is guaranteed to contain at least one word, and all words are guaranteed to be present in the given grid.
- All words are guaranteed to have at least two letters.
- You can assume each word is only once in the grid.
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:
Outputs are displayed as integer-lists.
Inputs:
REKNA
TAXIJ
RAREN
ATAEI
YCYAN
AIR
ANKER
EAT
CYAN
NINJA
RARE
TAXI
TRAY
XRAY
YEN
Output:
[2,1,2,1,1,3,1,2,1,2]
Inputs:
ABCD
EFGH
IJKL
MNOP
AFK
BCD
FC
PONM
Output:
[1,1,1,1]
Inputs:
WVERTICALL
ROOAFFLSAB
ACRILIATOA
NDODKONWDC
DRKESOODDK
OEEPZEGLIW
MSIIHOAERA
ALRKRRIRER
KODIDEDRCD
HELWSLEUTH
BACKWARD
DIAGONAL
FIND
HORIZONTAL
RANDOM
SEEK
SLEUTH
VERTICAL
WIKIPEDIA
WORDSEARCH
Output:
[1,1,2,1,1,4,1,1,1,3]
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:
[5,5,1,5,4,3,1,3,3,2,4,3,4,3,4,3]
word-search
challenges I've posted lately.) \$\endgroup\$'\n'
characters as additional items to the list in that case so it's still a 'grid'. \$\endgroup\$[A:6, I:2, R:4]
" be[A:6, I:2, R:3]
? \$\endgroup\$REKNA/TAXIJ/RAREN/ATAEI/YCYAN
contains{N:3,I:2,E:3}
. If i want spellNINE
, should I output 2 or 1 or 1.5? \$\endgroup\$