2021 day7/python: prettify

This commit is contained in:
Xiretza 2021-12-07 20:03:46 +01:00
parent e9a47c1863
commit 4ea70f38ea

View file

@ -3,13 +3,13 @@
import sys
if __name__ == '__main__':
inp = [int(i) for i in sys.stdin.readline().split(',')]
positions = [int(i) for i in sys.stdin.readline().split(',')]
def calc(f):
return min([
sum(f(abs(crab-i)) for crab in inp)
for i in range(min(inp), max(inp)+1)
])
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)
)
print(calc(lambda n: n))
print(calc(lambda n: n * (n+1)//2))