diff options
author | Thomas Voss <mail@thomasvoss.com> | 2022-12-03 06:41:26 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2022-12-03 06:41:26 +0100 |
commit | 50f590b6a4e5ac58b8e6baa655889097daef996d (patch) | |
tree | 51f58072badb74fee00ac171eb7e49d81e433876 | |
parent | 3be62b50f80c1d67d9867775242c33e88266fec8 (diff) |
Compress while remaining readable
-rwxr-xr-x | 2022/03/puzzle-1.py | 20 |
1 files changed, 6 insertions, 14 deletions
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__": |