From e9a47c1863436a6adb4f278f16b3504114862959 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Tue, 7 Dec 2021 06:24:59 +0100 Subject: [PATCH] 2021 day7/python: add solution --- 2021/day7/day7.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100755 2021/day7/day7.py diff --git a/2021/day7/day7.py b/2021/day7/day7.py new file mode 100755 index 0000000..ce6cb31 --- /dev/null +++ b/2021/day7/day7.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +import sys + +if __name__ == '__main__': + inp = [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) + ]) + + print(calc(lambda n: n)) + print(calc(lambda n: n * (n+1)//2))