import statistics def main(): with open("aoc7-1.txt", "r") as file: positions = [int(el) for el in file.read().strip().split(",")] # Gonna brute-force this and see what happens. # I bet I'm right and am just doing math wrong at one point. # By definition, the position we want will be between the lowest # and highest values in the list. # positions = [16,1,2,0,4,2,7,1,2,14] # mn, mx = min(positions), max(positions) # the outer bounds # movegroups = [] # for pos in positions: # movegroup = [] # for i in range(mn, mx+1): # movegroup.append(sum(range(abs(pos-i)+1))) # movegroups.append(movegroup) # movetotals = [] # for i in range((mx-mn)+1): # mt = [] # for move in movegroups: # mt.append(move[i]) # movetotals.append(sum(mt)) # print(min(movetotals), mn + movetotals.index(min(movetotals))) # print(statistics.mean(positions)) # I WAS RIGHT # Leaving the above in for posterity med = int(statistics.mean(positions)) moves = [sum(range(abs(pos-med)+1)) for pos in positions] print(sum(moves)) if __name__ == "__main__": main()