#!/usr/bin/env python import copy graph = dict() def calculate_paths(graph, current, small_seen, path): found = 0 for n in graph.get(current): if n == 'start': continue if n in small_seen: continue p = copy.copy(path) p.append(n) #print(p) #print("%s -> %s" % (current, n)) if n == 'end': found += 1 continue sm = copy.copy(small_seen) if n.lower() == n: sm.append(n) 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) print(calculate_paths(graph, 'start', [], ['start']))