From eec238335b8b18e735d61bf97c793ac7683c9dac Mon Sep 17 00:00:00 2001 From: setop Date: Wed, 8 Dec 2021 02:36:58 +0100 Subject: [PATCH] day 7 short --- d07/d07_1.py | 6 +----- d07/d07_2.py | 7 ++----- 2 files changed, 3 insertions(+), 10 deletions(-) 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)))