main.py 828 B

123456789101112131415161718192021222324252627282930
  1. import os, sys
  2. import numpy as np
  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 part1(input: list[str]):
  7. positions = np.fromstring(input[0], sep=",", dtype=int)
  8. a = np.abs(positions - np.floor(np.median(positions))).sum()
  9. print(f"The answer of part1 is: {a}")
  10. def part2(input: list[str]):
  11. positions = np.fromstring(input[0], sep=",", dtype=int)
  12. a = np.abs(positions - np.floor(np.mean(positions)))
  13. a = np.array([np.arange(n + 1).sum() for n in a]).sum()
  14. print(f"The answer of part2 is: {a}")
  15. if __name__ == "__main__":
  16. input, example = get_input(task_dir, 7)
  17. check_example(example, part1)
  18. check_example(example, part2)
  19. part1(input)
  20. part2(input)
  21. generate_readme(task_dir, 7)