-
-
Save badp/dc6c43148cc4798fcc4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
readMySpell = lambda x: x.partition("/")[0].partition("\t")[0].rstrip() | |
with open('/usr/share/myspell/dicts/en_GB.dic') as fobj: | |
words = {readMySpell(x) | |
for x in fobj.readlines()[2:] | |
if len(readMySpell(x)) >= 3} | |
prefixes = set(words) | |
for word in words: | |
for i in range(1, len(word)): | |
prefixes.add(word[:i]) | |
board = [] | |
line = None | |
while line != "": | |
line = input() | |
line.ljust(8) | |
board.append(line) | |
offsets = { | |
(-1, -1), (-1, 0), (-1, 1), | |
( 0, -1), ( 0, 1), | |
( 1, -1), ( 1, 0), ( 1, 1), | |
} | |
def move(pos, offset): | |
return tuple(map(sum, zip(pos, offset))) | |
def getWord(positions): | |
if positions[-1][0] < 0: | |
raise IndexError | |
if positions[-1][1] < 0: | |
raise IndexError | |
word = "".join(board[x][y] for (x,y) in positions) | |
if "." in word: | |
raise IndexError | |
return word | |
def navigateBoard(pos=(0,0), cur_word=None): | |
if cur_word is None: | |
cur_word = [pos] | |
for offset in offsets: | |
next = move(pos, offset) | |
if next in cur_word: | |
continue | |
try: | |
word = getWord(cur_word + [next]) | |
except IndexError: | |
continue | |
if word not in prefixes: | |
continue | |
if word in words: | |
yield word | |
for result in navigateBoard(next, cur_word + [next]): | |
yield result | |
results = {} | |
for x in range(len(board)): | |
for y in range(len(board[x])): | |
if board[x][y] != ".": | |
foo = sorted(navigateBoard((x,y)), key=len, reverse=True) | |
if foo: | |
results[(x+1,y+1)] = foo | |
import pprint | |
pprint.pprint(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment