From fa4ea4511a3547b68948abf404d8e56f9d8cb80d Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 7 Dec 2021 10:33:36 +0100 Subject: [PATCH] 2021: d07: ex1: add solution --- 2021/d07/ex1/ex1.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100755 2021/d07/ex1/ex1.py diff --git a/2021/d07/ex1/ex1.py b/2021/d07/ex1/ex1.py new file mode 100755 index 0000000..2b8a8a9 --- /dev/null +++ b/2021/d07/ex1/ex1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +import sys +from typing import List + + +def solve(input: List[str]) -> int: + nums = sorted(int(n) for n in input[0].split(",")) + median = nums[len(nums) // 2] + + return sum(abs(n - median) for n in nums) + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()