From 0e75993cb49f010304624ee1bb2ba358e379404a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 1 Dec 2024 08:47:28 +0000 Subject: [PATCH] 2024: d01: ex1: add solution --- 2024/d01/ex1/ex1.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 2024/d01/ex1/ex1.py diff --git a/2024/d01/ex1/ex1.py b/2024/d01/ex1/ex1.py new file mode 100755 index 0000000..df14c6a --- /dev/null +++ b/2024/d01/ex1/ex1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +import sys + + +def solve(input: list[str]) -> int: + def parse(input: list[str]) -> tuple[list[int], list[int]]: + left, right = [], [] + for line in input: + lhs, rhs = line.split() + left.append(int(lhs)) + right.append(int(rhs)) + return left, right + + left, right = parse(input) + return sum(abs(lhs - rhs) for lhs, rhs in zip(sorted(left), sorted(right))) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()