17_probe.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. import sys
  3. import re
  4. from itertools import islice
  5. from operator import or_
  6. from functools import reduce
  7. def xsteps(dx, lo, hi):
  8. x = steps = 0
  9. while x <= hi and dx:
  10. x += dx
  11. steps += 1
  12. dx -= 1
  13. if lo <= x <= hi:
  14. if dx:
  15. yield steps
  16. else:
  17. yield from range(steps, 300)
  18. def ysteps(dy, lo, hi):
  19. y = steps = 0
  20. while y >= lo:
  21. y += dy
  22. dy -= 1
  23. steps += 1
  24. if lo <= y <= hi:
  25. yield steps
  26. def bucketize(values, keys):
  27. buckets = {}
  28. for value in values:
  29. for key in keys(value):
  30. buckets.setdefault(key, []).append(value)
  31. return buckets
  32. def sim(x1, x2, y1, y2):
  33. ix = bucketize(range(1000), lambda dx: xsteps(dx, x1, x2))
  34. for dy in range(y1, 200):
  35. for steps in ysteps(dy, y1, y2):
  36. for dx in set(ix.get(steps, [])):
  37. yield dx, dy
  38. def maxy(vy):
  39. return 0 if vy < 0 else vy * (vy + 1) // 2
  40. x1, x2, y1, y2 = map(int, re.findall(r'-?\d+', sys.stdin.readline()))
  41. velocities = list(sim(x1, x2, y1, y2))
  42. print(max(maxy(vy) for vx, vy in velocities))
  43. print(len(velocities))