diff options
-rw-r--r-- | 7.py | 12 |
1 files changed, 12 insertions, 0 deletions
@@ -0,0 +1,12 @@ +from utils import open_day +from statistics import median + +crabs = list(map(int, open_day(7).read().split(','))) + +p = int(median(sorted(crabs))) +print(sum(abs(n - p) for n in crabs)) +s = sum(crabs) +l = len(crabs) +t = (s + sum(crab * l > s for crab in crabs)) // l +def sigma_n(n): return n * (n + 1) // 2 +print(sum(sigma_n(abs(n - t)) for n in crabs)) |