32 lines
802 B
Python
32 lines
802 B
Python
from math import inf
|
|
from tools.aoc import AOCDay
|
|
from tools.int_seq import triangular
|
|
from typing import Any, List
|
|
|
|
|
|
def getMinFuelUse(crabs: List[int], increased: bool = False) -> int:
|
|
minFuel = inf
|
|
for pos in range(min(crabs), max(crabs) + 1):
|
|
if increased:
|
|
fuel = sum(triangular(abs(pos - c)) for c in crabs)
|
|
else:
|
|
fuel = sum(abs(pos - c) for c in crabs)
|
|
|
|
if fuel < minFuel:
|
|
minFuel = fuel
|
|
|
|
return minFuel
|
|
|
|
|
|
class Day(AOCDay):
|
|
test_solutions_p1 = [37]
|
|
test_solutions_p2 = [168]
|
|
|
|
def part1(self) -> Any:
|
|
crabs = self.getInputAsArraySplit(",", int)
|
|
return getMinFuelUse(crabs)
|
|
|
|
def part2(self) -> Any:
|
|
crabs = self.getInputAsArraySplit(",", int)
|
|
return getMinFuelUse(crabs, True)
|