day 7
you can find the description here and you can find the code here
def cost(l, n, f):
return sum(f(e, n) for e in l)
for the first part, the solution is the median.
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.
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))