]> git.friedersdorff.com Git - max/advent_of_code_2021.git/blob - 7_2.py
Try day 14
[max/advent_of_code_2021.git] / 7_2.py
1 """Find the least fuel for the sub-crabs with triangle fuel consumption"""
2 with open("7_input.txt") as f:
3     crab_positions = [int(x) for x in f.read().split(",")]
4
5 fuel_costs = []
6 for target_position in range(max(crab_positions) + 1):
7     fuel_costs.append(0)
8     for crab_position in crab_positions:
9         delta = abs(target_position - crab_position)
10         fuel_costs[-1] += delta * (delta + 1)/2
11
12 min_fuel = min(fuel_costs)
13 min_fuel_position = fuel_costs.index(min_fuel)
14 print(min_fuel_position, min_fuel)