24_bridge.py 499 B

123456789101112131415161718
  1. #!/usr/bin/env python3
  2. import sys
  3. def grow(length, strength, end, rest):
  4. for comp in rest:
  5. if end in comp:
  6. other_end = comp[1] if end == comp[0] else comp[0]
  7. yield from grow(length + 1, strength + sum(comp),
  8. other_end, rest - {comp})
  9. yield length, strength
  10. components = {tuple(map(int, line.split('/'))) for line in sys.stdin}
  11. options = list(grow(0, 0, 0, components))
  12. print(max(s for l, s in options))
  13. print(max(options)[1])