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()