fork download
  1. import itertools
  2. lines="""\
  3. B1,C1,A,C4,B4
  4. B2,C2,A,C5,B5
  5. B3,C3,A,C6,B6
  6. D1,A,D4
  7. D2,A,D5
  8. D3,A,D6
  9. B1,D1,C2,D2,B3
  10. B2,D2,C3,D3,B4
  11. B3,D3,C4,D4,B5
  12. B4,D4,C5,D5,B6
  13. B5,D5,C6,D6,B1
  14. B6,D6,C1,D1,B2"""
  15. lines = frozenset(frozenset(line.split(",")) for line in lines.split("\n"))
  16. points = frozenset(itertools.chain.from_iterable(lines))
  17. cnt = 0
  18. fnd = dict()
  19. for a, b, c in itertools.combinations(points, 3):
  20. if (any(a in line and b in line and not c in line for line in lines) and
  21. any(a in line and c in line for line in lines) and
  22. any(b in line and c in line for line in lines)):
  23. cnt += 1
  24. key = ''.join(sorted((a[0], b[0], c[0])))
  25. fnd.setdefault(key, 0)
  26. fnd[key] += 1
  27. for k, v in sorted(fnd.items()):
  28. print("{:3d} × {}".format(v, k))
  29. print("{:3d} TOTAL".format(cnt))
Success #stdin #stdout 0.04s 9440KB
stdin
Standard input is empty
stdout
  6 × ABB
 12 × ABC
 24 × ABD
 12 × ACD
  6 × ADD
  2 × BBB
 12 × BBC
  6 × BBD
 12 × BCD
 12 × BDD
104 TOTAL