From 28a0de7bbdbc5c77b0d35c106d030e097fa56690 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Tue, 7 Dec 2021 10:54:13 -0500 Subject: [PATCH] day7: Fix averaging in pt b -- round instead of floor Without this, the example they give is slightly off: 16,1,2,0,4,2,7,1,2,14 -> 168 This intuitively makes sense, as the closest you can get to the average is the round()! --- 7/b.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/7/b.py b/7/b.py index f2aa416..47a7db4 100755 --- a/7/b.py +++ b/7/b.py @@ -9,14 +9,16 @@ # give us 3300 instead, which is significantly better. import sys + def cost(a, b): # Distance 3 = 1 + 2 + 3 = sum(range(abs))) return sum(range(abs(a - b) + 1)) + crabs = [] for l in sys.stdin: crabs = [int(x) for x in l.split(",")] -avg = int(sum(crabs) / len(crabs)) +avg = round(sum(crabs) / len(crabs)) print(sum([cost(x, avg) for x in crabs])) -- libgit2 1.8.1