advent-of-code/2021/day7/day7.py

16 lines
403 B
Python
Raw Normal View History

2021-12-07 06:24:59 +01:00
#!/usr/bin/env python
import sys
if __name__ == '__main__':
2021-12-07 20:03:46 +01:00
positions = [int(i) for i in sys.stdin.readline().split(',')]
2021-12-07 06:24:59 +01:00
2021-12-07 20:03:46 +01:00
def calc(movement_cost):
return min(
sum(movement_cost(abs(position-target)) for position in positions)
for target in range(min(positions), max(positions)+1)
)
2021-12-07 06:24:59 +01:00
print(calc(lambda n: n))
print(calc(lambda n: n * (n+1)//2))