Skip to content

Instantly share code, notes, and snippets.

@jkingsman
Created September 29, 2022 16:58
Show Gist options
  • Save jkingsman/a78eadae7b15de5a4d01b0cf79173e4c to your computer and use it in GitHub Desktop.
Save jkingsman/a78eadae7b15de5a4d01b0cf79173e4c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import copy
import multiprocessing
import enchant
MAX_WORD_LEN = 12
LOCALES = ['en_US', 'en_GB']
DICT_LOCALES = {}
for locale in LOCALES:
DICT_LOCALES[locale] = enchant.Dict(locale)
last_four_letter_combo = '____'
def get_letter_at_coordinate(coordinate, board):
if coordinate[0] < 0 or coordinate[1] < 0:
# running off board
return None
if coordinate[0] > (len(board) - 1):
return None
if coordinate[1] > (len(board[0]) - 1):
return None
return board[coordinate[0]][coordinate[1]]
def solve_from_initial_word(word_so_far, current_board, origin):
global last_four_letter_combo
found_words = set()
for row_adj in [-1, 0, 1]:
for col_adj in [-1, 0, 1]:
adjusted_coordinates = (origin[0] + row_adj, origin[1] + col_adj)
if get_letter_at_coordinate(adjusted_coordinates, current_board):
found_words.add(word_so_far)
word_so_far_local = word_so_far[:] + get_letter_at_coordinate(adjusted_coordinates, current_board)
if len(word_so_far_local) > MAX_WORD_LEN:
continue
if len(word_so_far_local) >= 4 and word_so_far_local[:4] != last_four_letter_combo:
last_four_letter_combo = word_so_far_local[:4]
print(last_four_letter_combo)
found_words.add(word_so_far_local)
current_board_local = copy.deepcopy(current_board)
current_board_local[adjusted_coordinates[0]][adjusted_coordinates[1]] = None
found_words = found_words | solve_from_initial_word(word_so_far_local, current_board_local, adjusted_coordinates)
return found_words
def get_words(board):
words = set()
for row in range(len(board)):
for col in range(len(board[0])):
# cycle through all starting coordinates
current_board = copy.deepcopy(board)
word_so_far = current_board[row][col]
if word_so_far:
# don't use words starting with None
words.add(word_so_far)
current_board[row][col] = None
origin = (row, col)
words = words | solve_from_initial_word(word_so_far, current_board, origin)
return set(words)
def expand_paramenter_packages(param_package):
return solve_from_initial_word(param_package[0], param_package[1], param_package[2])
def get_words_parallel(board):
parameter_packages = []
for row in range(len(board)):
for col in range(len(board[0])):
# cycle through all starting coordinates
current_board = copy.deepcopy(board)
word_so_far = current_board[row][col]
if word_so_far:
current_board[row][col] = None
origin = (row, col)
parameter_packages.append([word_so_far, current_board, origin])
pool = multiprocessing.Pool()
return(pool.map(expand_paramenter_packages, parameter_packages))
def check_against_dict(wordlist, locale):
return [word for word in wordlist if DICT_LOCALES[locale].check(word)]
def check_and_clean_list(word_list):
min_four_characters = [word for word in word_list if len(word) >= 4]
print(f"Word list contains {len(min_four_characters)} words")
print("Checking USA")
en_us_words = check_against_dict(min_four_characters, 'en_US')
print("Checking GB")
en_gb_words = check_against_dict(min_four_characters, 'en_GB')
all_words = set(list(en_gb_words + en_us_words))
return all_words
if __name__ == '__main__':
# board = [
# [None, 'R', 'A', 'U', None],
# ['E', 'I', 'N', 'R', 'C'],
# ['H', 'V', None, 'C', 'H'],
# [None, 'W', 'I', 'I', 'C'],
# [None, 'E', None, None, None]
# ]
board = [
No
]
words = get_words_parallel(board)
print("Flattening...")
flattened_words = [item for sublist in words for item in sublist]
print("Post processing...")
print(check_and_clean_list(flattened_words))
# words = get_words(board)
# print("Post processing...")
# print(check_and_clean_list(words))
# board = [
# ['G', 'M', 'X', 'Y', 'O'],
# ['B', 'A', 'E', 'T', 'R'],
# ['R', 'L', 'C', 'N', 'H'],
# ['A', 'S', 'W', 'I', 'G'],
# ['G', 'Z', 'Y', 'Z', 'E']
# ]
# words = get_words(board)
#
# min_four_characters = [word for word in words if len(word) >= 4]
# en_us_words = check_against_dict(min_four_characters, 'en_US')
# en_gb_words = check_against_dict(min_four_characters, 'en_GB')
# all_words = set(list(en_gb_words + en_us_words))
# print(all_words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment