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 година
1234567891011121314151617181920212223242526272829303132333435
  1. import itertools
  2. import pathlib
  3. import re
  4. text = open(0).read()
  5. [seed], *groups = [[[int(n) for n in ln.split()] for ln in group.split(':')[1].strip().splitlines()] for group in text.split('\n\n')]
  6. def forward(n):
  7. for group in groups:
  8. n = next((n + (b - a) for b, a, c in group if a <= n < a + c), n)
  9. return n
  10. ans1 = min([forward(n) for n in seed])
  11. print(ans1)
  12. ns = []
  13. for group in reversed(groups):
  14. ns += [n for b, a, c in group for n in [b, b + c - 1]]
  15. ns = [next((n - (b - a) for b, a, c in group if b <= n < b + c), n) for n in ns]
  16. ans2 = min(forward(n) for n in ns if any(a <= n < a + b for a, b in zip(seed[::2], seed[1::2])))
  17. print(ans2)
  18. # old method, slow
  19. def backward(n):
  20. for group in groups[::-1]:
  21. n = next((n - (b - a) for b, a, c in group if b <= n < b + c), n)
  22. return n
  23. pairs = [(a, a + b) for a, b in zip(seed[::2], seed[1::2])]
  24. next(n for n in itertools.count(ans2) if any(a <= backward(n) < b for a, b in pairs))