-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
48 lines (42 loc) · 1.22 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
48
import random
def load_dictionary(filename, encoding="ISO-8859-13"):
with open(filename, "r", encoding=encoding) as f:
words = f.readlines()
words = [word.strip() for word in words]
return words
def trie_set(trie, node, distance):
result = set()
for i in range(len(trie[node])):
if distance == 0:
result.add(i)
else:
result.update(trie_set(trie, trie[node][i], distance - 1))
return result
def traversal(n, is_icoordinate):
result = []
for k in range(n * 2):
for j in range(min(k, n), 0, -1):
i = k - j
if i < n and j <= i:
result.append(is_icoordinate * i + j)
return result
def word_square(trie, words, n):
grid = [[list(word) for word in words] for _ in range(n)]
for step in range(n * (n + 1) // 2):
i = traversal(n, True)[step]
j = traversal(n, False)[step]
options = trie_set(trie, grid[i][j][0], n - step - 1)
if len(options) == 0:
return False
grid[i][j].append(random.choice(options))
return True
def main():
trie = load_dictionary("8_length_words.txt")
words = load_dictionary("8_length_words.txt")
n = 8
if not word_square(trie, words, n):
print("No solution found")
else:
print(grid)
if __name__ == "__main__":
main()