-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12a.py
54 lines (41 loc) · 1.16 KB
/
day12a.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
from collections import defaultdict
f = open("input.txt", "r")
#f = open("testInput.txt", "r")
dat = f.read().splitlines()
adjList = defaultdict(list)
for line in dat:
src,dst = line.split("-")
if src == "start":
adjList["start"].append(dst)
elif dst == "start":
adjList["start"].append(src)
elif dst == "end":
adjList[src].append("end")
elif src == "end":
adjList[dst].append("end")
else:
adjList[src].append(dst)
adjList[dst].append(src)
# breadth first search from start
numRoutes = 0
#print(adjList)
#print()
def dfs(node, visited, curPath):
global numRoutes
#print(" "*d + node)
# if found end path
if node == "end":
#print(curPath)
numRoutes += 1
# if visiting this small tunnel for the first time
elif node.islower():
#print("visiting this small tunnel for the first time")
visited.add(node)
# explore neighbours of this node
for nei in adjList[node]:
if nei not in visited:
dfs(nei,visited, curPath+","+nei)
visited.discard(nei)
dfs("start", set(), "start")
#print()
print(numRoutes)