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):
= math.floor(rendezvous(positions))
r return fuel(positions, r, consumption)
import import_ipynb
import helper
= helper.read_file('2021_7.txt')
f
= list(map(int, f.rstrip().split(',')))
positions
least_fuel(positions,
statistics.median, =lambda x: x) consumption
352254
least_fuel(positions,
statistics.mean, =lambda x: x*(x+1)/2) consumption
99053143.0
Source code of the solution(s):