-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.py
68 lines (41 loc) · 1.33 KB
/
17.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
from lib import *
input = read_input(2016, 17).strip()
def get_state(path):
digest = hashlib.md5((input + path).encode()).hexdigest()
return [c >= "b" for c in digest[:4]] # up, down, left, right
def find_path():
queue = [("", 0, 0)]
while True:
path, x, y = queue.pop(0)
if x == y == 3:
return path
up, down, left, right = get_state(path)
if up and y > 0:
queue.append((path + "U", x, y - 1))
if down and y < 3:
queue.append((path + "D", x, y + 1))
if left and x > 0:
queue.append((path + "L", x - 1, y))
if right and x < 3:
queue.append((path + "R", x + 1, y))
print(find_path())
def find_path():
queue = [("", 0, 0)]
longest = ""
while queue:
path, x, y = queue.pop(0)
if x == y == 3:
if len(path) > len(longest):
longest = path
continue
up, down, left, right = get_state(path)
if up and y > 0:
queue.append((path + "U", x, y - 1))
if down and y < 3:
queue.append((path + "D", x, y + 1))
if left and x > 0:
queue.append((path + "L", x - 1, y))
if right and x < 3:
queue.append((path + "R", x + 1, y))
return longest
print(len(find_path()))