7.py 382 B

123456789101112131415161718
  1. import statistics
  2. from util import get_input
  3. input = get_input("7.input")
  4. crabs = [int(a) for a in input[0].split(",")]
  5. median = statistics.median(crabs)
  6. print(sum([abs(a - median) for a in crabs]))
  7. maxpos = max(crabs)
  8. def movecost(a, b):
  9. n = abs(a - b)
  10. return (n * (n + 1)) / 2
  11. print(min([sum([movecost(a, target) for a in crabs]) for target in range(maxpos + 1)]))