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-2.py 1.2KB

12345678910111213141516171819202122232425262728293031323334
  1. import statistics
  2. def main():
  3. with open("aoc7-1.txt", "r") as file:
  4. positions = [int(el) for el in file.read().strip().split(",")]
  5. # Gonna brute-force this and see what happens.
  6. # I bet I'm right and am just doing math wrong at one point.
  7. # By definition, the position we want will be between the lowest
  8. # and highest values in the list.
  9. # positions = [16,1,2,0,4,2,7,1,2,14]
  10. # mn, mx = min(positions), max(positions) # the outer bounds
  11. # movegroups = []
  12. # for pos in positions:
  13. # movegroup = []
  14. # for i in range(mn, mx+1):
  15. # movegroup.append(sum(range(abs(pos-i)+1)))
  16. # movegroups.append(movegroup)
  17. # movetotals = []
  18. # for i in range((mx-mn)+1):
  19. # mt = []
  20. # for move in movegroups:
  21. # mt.append(move[i])
  22. # movetotals.append(sum(mt))
  23. # print(min(movetotals), mn + movetotals.index(min(movetotals)))
  24. # print(statistics.mean(positions))
  25. # I WAS RIGHT
  26. # Leaving the above in for posterity
  27. med = int(statistics.mean(positions))
  28. moves = [sum(range(abs(pos-med)+1)) for pos in positions]
  29. print(sum(moves))
  30. if __name__ == "__main__":
  31. main()