From 96b6c799e7833569257c0233b21c37cd2d525da9 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 3 Dec 2022 07:48:31 +0100 Subject: [PATCH] 2022: d03: ex1: add solution --- 2022/d03/ex1/ex1.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 2022/d03/ex1/ex1.py diff --git a/2022/d03/ex1/ex1.py b/2022/d03/ex1/ex1.py new file mode 100755 index 0000000..95f341f --- /dev/null +++ b/2022/d03/ex1/ex1.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +import sys + + +def solve(input: list[str]) -> int: + def score(line: str) -> int: + common_items = set(line[: len(line) // 2]) & set(line[len(line) // 2 :]) + assert len(common_items) == 1 # Sanity check + common = common_items.pop() + return common.isupper() * 26 + ord(common.lower()) - ord("a") + 1 + + return sum(map(lambda line: score(line), input)) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()