diff --git a/d07/d07_1.py b/d07/d07_1.py index 61bbbda..6a8b1d1 100644 --- a/d07/d07_1.py +++ b/d07/d07_1.py @@ -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))) diff --git a/d07/d07_2.py b/d07/d07_2.py index 9513428..3fcd74a 100644 --- a/d07/d07_2.py +++ b/d07/d07_2.py @@ -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)))