-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
47 lines (34 loc) · 1.3 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from trie import get_trie, Trie
from copy import copy
import click
class Solver():
def __init__(self, puzzle_letters, center_letter):
self.letters = set()
self.letters.update(puzzle_letters)
self.letters.add(center_letter)
self.center_letter = center_letter
self.trie = get_trie()
def solve(self):
prefixes = copy(self.letters)
found_words = set()
while prefixes:
prefix = prefixes.pop()
if self.trie.word_exists(prefix) and self.center_letter in prefix and len(prefix) > 4:
found_words.add(prefix)
for letter in self.letters:
new_prefix = prefix + letter
if self.trie.get_prefix(new_prefix):
prefixes.add(new_prefix)
print(sorted(found_words, key=len, reverse=True))
@click.command()
@click.option("--letters", required=True, help="The letters surrounding the center letter")
@click.option("--center", required=True, help="The letter in the center of the puzzle")
def cli(letters, center):
center = center.lower()
letters = [l for l in letters.lower()]
click.echo("Spelling Bee")
click.echo(f"Letters: {letters}")
click.echo(f"Center: {center}")
Solver(letters, center).solve()
if __name__ == '__main__':
cli()