Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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()