-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (62 loc) · 1.08 KB
/
main.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
# INPUT
T = """xz-end
CJ-pt
pt-QW
hn-SP
pw-CJ
SP-end
hn-pt
GK-nj
fe-nj
CJ-nj
hn-ZZ
hn-start
hn-fe
ZZ-fe
SP-nj
SP-xz
ZZ-pt
nj-ZZ
start-ZZ
hn-GK
CJ-end
start-fe
CJ-xz"""
# parse transitions
ts = {}
for t in T.split("\n"):
o, d = t.split("-")
if o in ts:
ts[o].append(d)
else:
ts[o] = [d]
if d in ts:
ts[d].append(o)
else:
ts[d] = [o]
def valid(path, d):
if d in ["start", "end"] or d.upper() == d:
return True
if path.count(d) == 2:
return False
s = set()
for state in path:
if state in ["start", "end"] or state.upper() == state:
continue
if state in s and d in path:
return False
s.add(state)
return True
def explore(current, path):
path = path + [current]
if current == "end":
return 1
if current not in ts:
return 0
ds = [d for d in ts[current] if d != "start" and valid(path, d)]
path_count = 0
for d in ds:
path_count += explore(d, path)
return path_count
res = explore("start", [])
print(res)