You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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