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.

25 line
721B

  1. from functools import cmp_to_key
  2. def cmp(aa, bb):
  3. match aa, bb:
  4. case int(), int():
  5. return (aa > bb) - (aa < bb)
  6. case int(), list():
  7. return cmp([aa], bb)
  8. case list(), int():
  9. return cmp(aa, [bb])
  10. case list(), list():
  11. fallback = cmp(len(aa), len(bb))
  12. return next((res for res in map(cmp, aa, bb) if res), fallback)
  13. text = open(0).read()
  14. pairs = [[eval(ln) for ln in block.splitlines()] for block in text.strip().split('\n\n')]
  15. print(sum(i for i, (aa, bb) in enumerate(pairs, 1) if cmp(aa, bb) == -1))
  16. x, y = [2], [6]
  17. entries = sum(pairs, [x, y])
  18. entries.sort(key=cmp_to_key(cmp))
  19. print(entries.index(x) * entries.index(y))