12.py 977 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from util import get_input
  2. input = get_input("12.input")
  3. neighbour_map = {}
  4. def add_neighbour(a, b):
  5. ns = neighbour_map.get(a, [])
  6. ns.append(b)
  7. neighbour_map[a] = ns
  8. for line in input:
  9. [a, b] = line.split("-")
  10. add_neighbour(a, b)
  11. add_neighbour(b, a)
  12. def find_paths_1(current, prev):
  13. if current == "end":
  14. return 1
  15. return sum([find_paths_1(next, prev + [current]) for next in neighbour_map[current] if not (next.islower() and next in prev)])
  16. print(find_paths_1("start", ["start"]))
  17. def ndup(ls):
  18. curr = []
  19. dups = 0
  20. for l in ls:
  21. if l in curr and l.islower():
  22. dups += 1
  23. curr.append(l)
  24. return dups
  25. def find_paths_2(current, prev):
  26. if current == "end":
  27. return 1
  28. return sum([find_paths_2(next, prev + [current]) for next in neighbour_map[current] if not ((next.islower() and next in prev and ndup(prev + [current]) > 0) or next == "start")])
  29. print(find_paths_2("start", []))