From 50f590b6a4e5ac58b8e6baa655889097daef996d Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 3 Dec 2022 06:41:26 +0100 Subject: Compress while remaining readable --- 2022/03/puzzle-1.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to '2022/03/puzzle-1.py') diff --git a/2022/03/puzzle-1.py b/2022/03/puzzle-1.py index 713e282..d0a788d 100755 --- a/2022/03/puzzle-1.py +++ b/2022/03/puzzle-1.py @@ -5,22 +5,14 @@ def cut(s: str) -> (str, str): return s[:n], s[n:] +def process(line: str) -> int: + car, cdr = cut(line) + c = set(car).intersection(cdr).pop() + return ord(c) - ord('a') + 1 if c >= 'a' else ord(c) - ord('A') + 27 + def main() -> None: with open("input", "r") as f: - data = f.readlines() - - acc = 0 - for line in data: - car, cdr = cut(line) - for c in car: - if cdr.find(c) != -1: - if "a" <= c <= "z": - acc += ord(c) - ord("a") + 1 - else: - acc += ord(c) - ord("A") + 27 - break - - print(acc) + print(sum(process(line) for line in f.readlines())) if __name__ == "__main__": -- cgit v1.2.3