-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordhunt.py
executable file
·120 lines (85 loc) · 3.19 KB
/
wordhunt.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
import itertools
from trie import TrieNode
from common import ingest_dictionary
root_node = TrieNode()
matched_word_dict = {}
def setup_trie():
global trie
print("Setting up trie...")
for x in ingest_dictionary():
root_node.insert(x.lower().strip())
print("Done with trie!")
def matching_words(prefix, banned_idxs):
node = root_node.node_for_prefix(prefix)
if not node:
return False
if node.word_end:
if len(prefix) not in matched_word_dict:
matched_word_dict[len(prefix)] = []
matched_word_dict[len(prefix)].append((prefix, tuple(banned_idxs)))
return True
def solve_wordhunt_for_index(array, first_idx, second_idx, banned_idxs=None, prefix=''):
if banned_idxs is None:
banned_idxs = []
current_letter = array[first_idx][second_idx]
new_prefix = prefix + current_letter
banned_idxs.append((first_idx, second_idx))
if not matching_words(new_prefix, banned_idxs):
return
for i in range(-1, 2):
for j in range(-1, 2):
new_first_idx = first_idx + i
new_second_idx = second_idx + j
if i == j == 0:
continue
if new_first_idx < 0 or new_second_idx < 0:
continue
if new_first_idx >= len(array) or new_second_idx >= len(array[new_first_idx]):
continue
if (new_first_idx, new_second_idx) in banned_idxs:
continue
solve_wordhunt_for_index(array, new_first_idx, new_second_idx, banned_idxs.copy(), new_prefix)
def get_board_from_user():
print("Type each character seperated by a space, with each row seperated by a newline")
word_array = []
while True:
try:
line = input()
except EOFError:
break
line_chars = line.strip().split()
word_array.append(line_chars)
print("\n")
return word_array
def print_output(word_array):
for length, words in sorted(matched_word_dict.items()):
if length < 5:
words_only = {word[0] for word in words}
print(f"Words for count {length}: {words_only}\n")
else:
print(f"Words for count {length}: \n")
deduplicated_words = dict(words).items()
for word in deduplicated_words:
print(f"{word[0]}:")
for i in range(0, len(word_array[0])):
for j in range(0, len(word_array)):
if (i, j) == word[1][0]:
print("🟢", end="")
elif (i, j) == word[1][len(word[1]) - 1]:
print("🏁", end="")
elif (i, j) in word[1]:
print("🟨", end="")
else:
print("⬜", end="")
print("")
print("\n\n")
def main():
setup_trie()
word_array = get_board_from_user()
for i in range(0, len(word_array)):
for j in range(0, len(word_array[i])):
solve_wordhunt_for_index(word_array, i, j)
print_output(word_array)
if __name__ == "__main__":
main()