12.py 857 B

1234567891011121314151617181920212223242526272829
  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 find_paths_2(current, prev, duped=False):
  18. if current == "end":
  19. return 1
  20. return sum([find_paths_2(next, prev + [current], duped or (next.islower() and next in prev)) for next in neighbour_map[current] if not ((duped and next.islower() and (next in prev)) or next == "start")])
  21. print(find_paths_2("start", []))