blob: 945706895fe393088e2ad5cf3f5f25b3606bf220 (
plain)
1
2
3
4
5
6
7
8
9
10
11
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))
|