Introduction:
Pete likes doing word search puzzles. Despite that, he has trouble searching for words vertically, (anti-)diagonally, or reversed. Because of that, he'll always search for the words left-to-right, and rotates the entire puzzle in increments of 45 degrees clockwise.
In addition to that, he'll also always search for the words in (alphabetical) order.
Challenge:
Given a word search grid and a list of (alphabetically ordered) words, output how many rotations are necessary to find all the words.
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.
For example:
Grid:
ABCD
EFGH
IJKL
MNOP
Words:
AFK
BCD
FC
PONM
Here, the first string AFK
can be found diagonally from the topleft-to-bottomright; the second word BCD
horizontally from the left-to-right; etc.:
AFK BCD FC PONM
aBCD Abcd ABcD ABCD
EfGH EFGH EfGH EFGH
IJkL IJKL IJKL IJKL
MNOP MNOP MNOP mnop
Challenge example:
If we take the same example grid and list of words above, we find AFK
after 7 clockwise rotations of 45 degrees; BCD
after 1 more rotation; FC
after 1 more rotation; and PONM
after 3 more rotations. So the result for the example grid and four words will be 12
(7+1+1+3).
Here a visual representation of these steps:
A M P D
E B N I L O C H
ABCD I F C MIEA O J E PONM H K N DHLP B G L
EFGH → M J G D → NJFB → P K F A → LKJI → D G J M → CGKO → A F K P ⮌
IJKL N K H OKGC L G B HGFE C F I BFJN E J O
MNOP O L PLHD H C DCBA B E AEIM I N
P D A M
1 2 3 4 5 6 7 A F K
BCD 8
9 F C
10 11 12 PONM
Challenge rules:
- Input can be in any reasonable format. The input-grid could be a character-matrix; list/stream of lines; taken from STDIN; etc. The same applies to the list of words.
- The inputs are guaranteed to only contain regular letters (in the same casing, unless you prefer otherwise).
- You're allowed to take the inputs in uppercase; lowercase; mixed case; or even as 0- or 1-based alphabet indices as integers if you'd want.
- You can assume the input-words will always be in alphabetical order.
- You can assume the words will always be present in the grid, and will only occur once.
- All words are guaranteed to have at least two letters (because single-letter words could be found in multiple directions, adding annoying edge cases).
- You can assume neither the grid nor list of words will be empty.
- No need to deal with the remaining letters or partial overlapping of word. Just look for the words one at a time and output the amount of 45 degree clockwise rotations that are necessary.
- The grid will not necessarily be a square, but it's guaranteed to be a rectangle.
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:
ABCD
EFGH
IJKL
MNOP
AFK
BCD
FC
PONM
Output: 12
Inputs:
ABC
AB
ABC
BC
Output: 0
Inputs:
AB
BA
Output: 4
Inputs:
WVERTICALL
ROOAFFLSAB
ACRILIATOA
NDODKONWDC
DRKESOODDK
OEEPZEGLIW
MSIIHOAERA
ALRKRRIRER
KODIDEDRCD
HELWSLEUTH
BACKWARD
DIAGONAL
FIND
HORIZONTAL
RANDOM
SEEK
SLEUTH
VERTICAL
WIKIPEDIA
WORDSEARCH
Output: 39