aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-12-03 06:41:26 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-12-03 06:41:26 +0100
commit50f590b6a4e5ac58b8e6baa655889097daef996d (patch)
tree51f58072badb74fee00ac171eb7e49d81e433876 /2022
parent3be62b50f80c1d67d9867775242c33e88266fec8 (diff)
Compress while remaining readable
Diffstat (limited to '2022')
-rwxr-xr-x2022/03/puzzle-1.py20
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__":