#Skip to menu

Day 7, Year 2021: The Treachery of Whales

First read the problem description.
import statistics, math

def fuel(positions, rendezvous, consumption):
    return sum(map(lambda x: consumption(abs(x-rendezvous)),
                   positions))

def least_fuel(positions, rendezvous, consumption):
    r = math.floor(rendezvous(positions))  
    return fuel(positions, r, consumption)
import import_ipynb
import helper
f = helper.read_file('2021_7.txt')

positions = list(map(int, f.rstrip().split(',')))
least_fuel(positions, 
           statistics.median, 
           consumption=lambda x: x)
352254
least_fuel(positions,
           statistics.mean, 
           consumption=lambda x: x*(x+1)/2)
99053143.0

Source code of the solution(s):