6.py 729 B

12345678910111213141516171819202122232425262728293031323334353637
  1. lines = []
  2. with open("6.input") as f:
  3. for line in f.readlines():
  4. lines.append(line.strip())
  5. yesanswers = set()
  6. yes_count = 0
  7. for line in lines:
  8. if line == "":
  9. yes_count += len(yesanswers)
  10. yesanswers = set()
  11. else:
  12. yesanswers = yesanswers.union(set(line))
  13. yes_count += len(yesanswers)
  14. print("Answer 1: {}".format(yes_count))
  15. yes_count = 0
  16. yesanswers = None
  17. for line in lines:
  18. if line == "":
  19. yes_count += len(yesanswers)
  20. yesanswers = None
  21. else:
  22. if yesanswers is None:
  23. yesanswers = set(line)
  24. else:
  25. yesanswers = yesanswers.intersection(set(line))
  26. yes_count += len(yesanswers)
  27. print("Answer 2: {}".format(yes_count))