#420 · Games and puzzles · Advanced · 160 XP · about 35 min
A word puzzle shows a grid of letters, given as a list of equal-length strings. A word can be traced in the grid if you can start on a square holding its first letter and reach each next letter on a square directly above, below, left or right of the previous one, never using the same square twice within the word.
Write find_words(grid, words) that returns the alphabetically sorted list of the words in words that can be traced. Each word appears once in the result, even if words lists it twice.
grid = [ 'snow', 'tale', 'ripe', ] find_words(grid, ['snow', 'stair', 'plan', 'peel', 'tail', 'wee', 'owl', 'pale']) # returns ['peel', 'plan', 'snow', 'stair', 'wee'] find_words(['ab', 'ba'], ['ababa', 'abab', 'bab']) # returns ['abab', 'bab']
In the first grid, "stair" goes down from s to t, right to a, down to i and left to r. "tail" cannot be traced because the only i is not next to the only l. In the second grid "ababa" would need a fifth square, and squares cannot be used twice.
words has 0 to 5,000 words, each with 1 to 10 lowercase letters.First solves only, ranked by stars and then time.