day 7 short

This commit is contained in:
setop 2021-12-08 02:36:58 +01:00
parent c2321a1a72
commit eec238335b
2 changed files with 3 additions and 10 deletions

View File

@ -1,6 +1,2 @@
L=list(map(int,__import__("sys").stdin.read().split(",")))
for i in range(min(L), max(L)+1):
R=sum(abs(l-i) for l in L)
print(R,i)
# python3 d07_2.py < input | sort -n | head -1
print(min(sum(abs(l-i) for l in L) for i in range(min(L), max(L)+1)))

View File

@ -1,8 +1,5 @@
L=list(map(int,__import__("sys").stdin.read().split(",")))
def cost(a,b):
n=abs(a-b)
return ((n+1)*n)/2
for i in range(min(L), max(L)+1):
R=sum(cost(l,i) for l in L)
print(R,i)
# python3 d07_2.py < input | sort -n | head -1
return ((n+1)*n)//2
print(min(sum(cost(l,i) for l in L) for i in range(min(L), max(L)+1)))