23 lines
631 B
Python
23 lines
631 B
Python
from typing import Any, Iterator
|
|
|
|
from ..base import BaseSolver
|
|
|
|
|
|
class Solver(BaseSolver):
|
|
def solve(self, input: str) -> Iterator[Any]:
|
|
positions = [int(c) for c in input.split(",")]
|
|
|
|
min_position, max_position = min(positions), max(positions)
|
|
|
|
# part 1
|
|
yield min(
|
|
sum(abs(p - position) for p in positions)
|
|
for position in range(min_position, max_position + 1)
|
|
)
|
|
|
|
# part 2
|
|
yield min(
|
|
sum(abs(p - position) * (abs(p - position) + 1) // 2 for p in positions)
|
|
for position in range(min_position, max_position + 1)
|
|
)
|