-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_hunt.py
48 lines (35 loc) · 978 Bytes
/
word_hunt.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
from time import perf_counter
t0 = perf_counter()
words = open('word_hunt.txt').read().split()
class Trie(dict):
def insert(t, s):
for c in s:
t[c] = t = t[c] if c in t else Trie()
t[None] = True
trie = Trie() # {'a': {'b': {'c': {None: True}}}}
for word in words:
trie.insert(word)
t1 = perf_counter()
print(f'Loaded Trie in {t1-t0:.3f}s')
R = C = 4
A = [list(input().strip()) for r in range(R)]
t0 = perf_counter()
res = set()
def dfs(r, c, t, s, v):
if r < 0 or r >= R or c < 0 or c >= C: return
if A[r][c] not in t: return
if v & 1<<r*C+c: return
t = t[A[r][c]]
s += A[r][c]
v |= 1<<r*C+c
if None in t: res.add(s)
for i in range(r-1, r+2):
for j in range(c-1, c+2):
dfs(i, j, t, s, v)
for r in range(R):
for c in range(C):
dfs(r, c, trie, '', 0)
res = sorted(res, key=len, reverse=True)
print(' '.join(res))
t1 = perf_counter()
print(f'Solved in {t1-t0:.3f}s')