main.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from doctest import Example
  2. import os, sys, re
  3. task_dir = os.path.dirname(__file__)
  4. sys.path.append(f'{task_dir}/..')
  5. from get_tasks import get_input, generate_readme, check_example
  6. def hit(position, target):
  7. x, y = position
  8. x1, x2, y1, y2 = target
  9. return True if x <= x2 and x >= x1 and y >= y1 and y <= y2 else False
  10. def launch_prob(target):
  11. succes = {}
  12. succes_init_velocity = []
  13. for i in range(1, target[1]+1):
  14. for j in range(target[2], -target[2]):
  15. init_velocity = i, j
  16. hor, ver = i, j
  17. hy = 0
  18. x, y = 0, 0
  19. while not (apple := hit((x, y), target)):
  20. py = y
  21. x += hor; y += ver
  22. if py > y and hy == 0: hy = py
  23. if hor < 0: hor += 1
  24. elif hor > 0: hor -= 1
  25. else: pass
  26. ver -= 1
  27. if (x > target[1]) or (y < target[2]): break
  28. if apple:
  29. succes[hy] = init_velocity
  30. succes_init_velocity.append(init_velocity)
  31. return succes, succes_init_velocity
  32. def part1(input: list[str]):
  33. target = [int(c) for c in re.findall('-?\d+', input[0])]
  34. hy, _ = launch_prob(target)
  35. print("The answer of part1 is:", max(hy))
  36. def part2(input: list[str]):
  37. target = [int(c) for c in re.findall('-?\d+', input[0])]
  38. _, v = launch_prob(target)
  39. print("The answer of part1 is:", len(v))
  40. if __name__ == "__main__":
  41. input, example = get_input(task_dir, 17)
  42. check_example(example, part1)
  43. check_example(example, part2)
  44. part1(input)
  45. part2(input)
  46. generate_readme(task_dir, 17)