11
1
The Challenge
Given a list of words and a grid of letters, your job is to determine which of the given words can be found on the grid in all 8 directions (forward, backward, up, down, and the 4 diagonal directions), much like a word search. The grid is toroidal, so the grid wraps around on the edges. Letters can be reused with this wrap-around property.
Wrapping around for diagonals behaves as you might expect. On a 4x6 grid going diagonally down and right, you would expect movement to look like this:
9 . 5 . 1 .
. . . 6 . 2
3 . . . 7 .
. 4 . . . 8
For an example of a wrap-around reuse word:
S E A H O R
Would be able to make SEAHORSE
Example Test Cases
Given this grid and the word list [ANT, COW, PIG]:
A N T
W C O
P G I
The result would be [ANT, COW, PIG]
Given this grid and the word list [CAT, DOG, DUCK, GOOSE, PUMPKIN]:
N O O D M E
O I O C U U
P G K O T C
K A U P S K
The result would be [DOG, DUCK, PUMPKIN]
Given this grid and the word list [TACO, BEEF, PORK]:
T A C B
A P O R
K E E F
B F C P
The result would be an empty list
Given this grid and the word list [ACHE, ASK, BAD, BOOK, COOK, CROW, GAS, GOOD, LESS, MARK, MASK, MEAL, SAME, SEAHORSE, SELL, SHOW, SLACK, WOOD]:
G S L A C K
H A E S R O
C M S K O O
A K S D W B
The result would be [ASK, BOOK, COOK, CROW, GAS, GOOD, LESS, MASK, SEAHORSE, SHOW, SLACK]
Given this grid and a list of all English words that are at least 3 letters long:
A R T U P
N T U B O
E S R O H
Q E T I U
E A T D P
The result would be [AEQ, AES, ANE, ART, ARTS, ATRIP, BOID, BON, BREE, BUD, BUT, DUB, DUE, DUET, EAN, EAR, EAT, EER, EST, HES, HIT, HOP, HOR, HORS, HORSE, HUP, NAE, NOB, NOBUT, ONT, OOT, OPP, ORS, OUR, OUT, PAR, PART, PAT, PEA, PEAT, PEE, PEER, PIR, POH, PUT, QAT, QUI, QUIT, QUITE, RAP, REE, RIP, RIPA, RTI, RUT, SEA, SEAR, STD, STR, STRA, STRAE, SUU, TAA, TAE, TAP, TAPIR, TEE, TIU, TOO, TRA, TRAP, TRI, TRIP, TUB, TUP, TUR, UDI, UIT, URE, UTE]
Rules
- No exploiting standard loopholes
- This is code-golf, so shortest code wins
- Input and output may be in any convenient format
- You may assume that both the grid and the words list will use only capital letters
- The output order of the words does not matter
- You do not have to, but may, filter out duplicates
Related. – Oliver – 2018-05-05T05:20:44.377
@Lynn Even though I can think of one other approach, I agree, it too reduces to WSP. – Erik the Outgolfer – 2018-05-05T14:10:26.007
Where were all of you when this was in the sandbox? Just saying. It got 3 upvotes there. – Beefster – 2018-05-08T22:25:12.813
1This is clearly not a duplicate. Although being on a torus doesn't sound like much of a change, most implementations will be significantly different. Here's my Python 3 approach anyway
lambda s,d:[w for w in d if any(r(s,w,x,y,i,j)for x in range(l(s[0]))for y in range(l(s))for i in[-1,0,1]for j in[-1,0,1]if i*i+j*j)];r=lambda s,w,x,y,i,j:w==""or s[y%l(s)][x%l(s[0])]==w[0]and r(s,w[1:],x+i,y+j,i,j);l=len
– Matthew Jensen – 2019-12-17T23:01:27.107