You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 line
815B

  1. from collections import defaultdict, Counter
  2. def solve(graph, part2=False):
  3. edge = [('start',)]
  4. final = []
  5. while edge:
  6. edge = [
  7. route + (new,)
  8. for route in edge
  9. for new in graph[route[-1]]
  10. if new.isupper() or route.count(new) == 0
  11. or part2 & (
  12. new != 'start'
  13. and route.count(new) == 1
  14. and max(Counter(filter(str.islower, route)).values()) == 1
  15. )
  16. ]
  17. final.extend(el for el in edge if el[-1] == 'end')
  18. edge = [el for el in edge if el[-1] != 'end']
  19. return len(final)
  20. graph = defaultdict(set)
  21. for ln in open(0):
  22. a, b = ln[:-1].split('-')
  23. graph[a].add(b)
  24. graph[b].add(a)
  25. graph = dict(graph)
  26. print(solve(graph))
  27. print(solve(graph, True))