b.py (701B) [raw]
1 #!/usr/bin/env python3 2 import sys 3 import re 4 from collections import defaultdict 5 6 E = defaultdict(list) 7 8 9 # Parse in edges 10 for l in sys.stdin: 11 a, b = l.strip().split('-') 12 E[a].append(b) 13 E[b].append(a) 14 15 smallre = re.compile('[a-z]+') 16 def issmall(n): 17 return smallre.match(n) 18 19 paths = 0 20 P = [] 21 def search(n, path, has_doubled): 22 global paths 23 global P 24 if n == 'end': 25 paths += 1 26 P.append(path + n) 27 return 28 if issmall(n) and n in path: 29 if has_doubled: 30 return 31 else: 32 has_doubled = True 33 for c in E[n]: 34 if c != 'start': 35 search(c, path + n, has_doubled) 36 37 search("start", "", False) 38 print(paths)