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.

преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
12345678910111213141516171819202122232425262728293031323334353637
  1. import itertools
  2. import pathlib
  3. import re
  4. def forward(n, groups):
  5. for group in groups:
  6. for b, a, c in group:
  7. if a <= n < a + c:
  8. n = n + (b - a)
  9. break
  10. return n
  11. def backward(n, groups):
  12. for group in reversed(groups):
  13. for b, a, c in group:
  14. if b <= n < b + c:
  15. n = n - (b - a)
  16. break
  17. return n
  18. text = open(0).read()
  19. to_matrix = lambda string: [[int(n) for n in ln.split()] for ln in string.splitlines()]
  20. [seed], *groups = [to_matrix(string.split(':')[1].strip()) for string in text.split('\n\n')]
  21. ans1 = min([forward(n, groups) for n in seed])
  22. print(ans1)
  23. ns = []
  24. pairs = [(a, a + b) for a, b in zip(seed[::2], seed[1::2])]
  25. for group in reversed(groups):
  26. ns += [n for b, a, c in group for n in [b, b + c - 1]]
  27. ns = [backward(n, [group]) for n in ns]
  28. ans2 = min(forward(n, groups) for n in ns if any(a <= n < b for a, b in pairs))
  29. print(ans2)