adventofcode-2021/07/solve02.py

25 lines
465 B
Python
Raw Normal View History

2021-12-07 06:52:13 +01:00
#!/usr/bin/python
import sys
from collections import defaultdict
with open("input01.txt","r") as f:
data = [int(i) for i in f.readline().split(',')]
count = defaultdict(int)
for i in data:
count[i] += 1
pos_min = min([i for i in count.keys()])
pos_max = max([i for i in count.keys()])
results = []
for pos in range(pos_min, pos_max+1):
tmp = 0
for i in count.keys():
a=abs(pos-i)
tmp += a*(a+1)/2*count[i]
results.append(tmp)
print(min(results))