-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17-2.py
50 lines (34 loc) · 927 Bytes
/
17-2.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
from hashlib import md5
from functools import lru_cache
from itertools import product
data = 'ihgpwlah'.encode()
moves_diff = {
'U': (0, -1),
'D': (0, 1),
'L': (-1, 0),
'R': (1, 0),
}
@lru_cache(None)
def validate_path(path):
x, y = 0, 0
for i in range(1, len(path)):
h = md5(data + path[:i].encode()).hexdigest()
if h[list(moves_diff).index(move)] not in 'bcdef':
return False
dx, dy = moves_diff[move]
nx, ny = x + dx, y + dy
if not (0 <= nx < 4 and 0 <= ny < 4):
return False
x, y = nx, ny
return True
valid_paths = []
while True:
valid_moves = []
for path in product(*valid_paths):
for move in 'UDLR':
if validate_path(''.join(path + (move,))):
valid_moves.append(move)
if not valid_moves:
break
valid_paths.append(valid_moves)
print(valid_paths)