part2.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import argparse
  2. import functools
  3. from collections import defaultdict
  4. from typing import Set, List, Dict
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument("ifile", type=argparse.FileType('r'))
  7. args = parser.parse_args()
  8. graph: defaultdict[str, Set[str]] = defaultdict(set)
  9. for edge in args.ifile.readlines():
  10. points = edge.strip().split('-')
  11. graph[points[0]].add(points[1])
  12. graph[points[1]].add(points[0])
  13. def depthSearch(point, path: List[str], smalls: Set[str], completedPaths: List[List[str]], graph: Dict[str, set[str]], twiced: bool):
  14. path.extend([point])
  15. if point == 'end':
  16. completedPaths.append(path)
  17. return
  18. for neighbor in filter(lambda p: p != "start", graph[point]):
  19. if neighbor.islower():
  20. if neighbor in smalls:
  21. if not twiced:
  22. depthSearch(neighbor, path, smalls, completedPaths, graph, True)
  23. else:
  24. smalls.add(neighbor)
  25. depthSearch(neighbor, path, smalls, completedPaths, graph, twiced)
  26. smalls.remove(neighbor)
  27. path.pop()
  28. else:
  29. depthSearch(neighbor, path, smalls, completedPaths, graph, twiced)
  30. path.pop()
  31. done_paths = []
  32. depthSearch("start", [], set(), done_paths, graph, False)
  33. print(len(done_paths))