Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

aoc7-1.py 536B

2 år sedan
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()