Advent-of-Code/2021/7/main2.py

44 lines
901 B
Python
Raw Permalink Normal View History

2021-12-07 15:51:22 -05:00
values = []
with open('data', 'r') as fp:
values = fp.readline().strip().split(',')
for i in range(0, len(values)):
values[i] = int(values[i])
values.sort()
index = int(len(values) / 2)
total = 0
for i in range(0, len(values)):
if(i != index):
total += abs(values[i] - values[index])
print(total)
min = min(values)
max = max(values)
possible = []
for i in range(min, max):
# # DEBUG(dev):
# print()
total = 0
for j in range(0, len(values)):
distance = abs(values[j] - i)
total += distance * ((distance + 1) / 2)
# # DEBUG(dev):
# print(values[j], '-', i, distance * ((distance + 1) / 2))
# # DEBUG(dev):
# print(total)
possible.append(total)
min_i = 0
min = possible[min_i]
for i in range(1, len(possible)):
if(possible[i] < min):
min = possible[i]
min_i = i
print(min_i, values[min_i], min)