選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

aoc7-1.py 536B

1234567891011121314151617
  1. import statistics
  2. def main():
  3. """
  4. Find the position that requires the fewest total moves for each crab sub to get there.
  5. This yields to an insight: given a list of numbers, the value that requires the
  6. fewest total "moves" to get there is the median.
  7. """
  8. with open("aoc7-1.txt", "r") as file:
  9. positions = [int(el) for el in file.read().strip().split(",")]
  10. med = statistics.median(positions)
  11. moves = [abs(pos-med) for pos in positions]
  12. print(sum(moves))
  13. if __name__ == "__main__":
  14. main()