#!/usr/bin/env python import copy from collections import defaultdict graph = dict() def calculate_paths(graph, current, small_seen, path): if current == 'end': return 1 found = 0 for n in sorted(graph.get(current)): if n == 'start': continue # already seen, check if there is any with count if small_seen[n] > 0: tmp = max(small_seen.values()) if tmp > 1: continue p = copy.copy(path) p.append(n) #print("%s -> %s" % (current, n)) sm = copy.copy(small_seen) if n.lower() == n: sm[n] += 1 found += calculate_paths(graph, n, sm, p) return found with open("input01.txt","r") as f: for line in f: (x,y) = line.strip().split("-",2) if x not in graph: graph[x] = [] graph[x].append(y) if y not in graph: graph[y] = [] graph[y].append(x) #print(graph) small_seen = defaultdict(int) print(calculate_paths(graph, 'start', small_seen, ['start']))