day 07
the treachery of whales
python
def cost(l, n, f):
return sum(f(e, n) for e in l)for the first part, the solution is the median.
python
def median(l):
return l[len(l) >> 1]
def part1(l):
m = median(l)
return cost(l, m, lambda x, y: abs(x - y))for the second part, the solution is the average.
python
def part2(l):
avg = sum(l) / len(l)
f = lambda x, y: gauss(abs(x - y))
return min(cost(l, ceil(avg), f), cost(l, floor(avg), f))