-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.py
90 lines (81 loc) · 1.79 KB
/
Day12.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
79
80
81
82
83
84
85
86
87
88
89
input="""start-YA
ps-yq
zt-mu
JS-yi
yq-VJ
QT-ps
start-yq
YA-yi
start-nf
nf-YA
nf-JS
JS-ez
yq-JS
ps-JS
ps-yi
yq-nf
QT-yi
end-QT
nf-yi
zt-QT
end-ez
yq-YA
end-JS"""
input_test = """start-A
start-b
A-c
A-b
b-d
A-end
b-end"""
def find_path(place, path, graph):
if place == 'end':
return 1
res = 0
for x in graph[place]:
if x.isupper() or x not in path:
path_next = path.copy()
path_next.append(x)
res += find_path(x, path_next, graph)
return res
def first_star():
input_list = input.split('\n')
graph = {}
for x in input_list:
y = x.split('-')
if y[0] not in graph.keys():
graph[y[0]] = []
graph[y[0]].append(y[1])
if y[1] not in graph.keys():
graph[y[1]] = []
graph[y[1]].append(y[0])
print(f"First star: {find_path('start', ['start'], graph)}")
def find_path2(place, path, graph, already2):
if place == 'end':
return 1
res = 0
for x in graph[place]:
if x.isupper() or x not in path:
path_next = path.copy()
path_next.append(x)
res += find_path2(x, path_next, graph, already2)
elif not already2 and x != 'start':
path_next = path.copy()
path_next.append(x)
res += find_path2(x, path_next, graph, True)
return res
def second_star():
input_list = input.split('\n')
graph = {}
for x in input_list:
y = x.split('-')
if y[0] not in graph.keys():
graph[y[0]] = []
graph[y[0]].append(y[1])
if y[1] not in graph.keys():
graph[y[1]] = []
graph[y[1]].append(y[0])
print(f"Second star: {find_path2('start', ['start'], graph, False)}")
if __name__ == '__main__':
first_star()
second_star()