From ccc1ac4b643dfa692fe0c37f57e9493494245ecb Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 3 Dec 2022 06:47:51 +0100 Subject: Same as last commit --- 2022/03/puzzle-2.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) (limited to '2022/03') diff --git a/2022/03/puzzle-2.py b/2022/03/puzzle-2.py index 7be3e84..4c25b52 100755 --- a/2022/03/puzzle-2.py +++ b/2022/03/puzzle-2.py @@ -8,23 +8,15 @@ def grouper(xs: list[str], n: int) -> list[list[str]]: return zip_longest(*args) -def main() -> None: - with open("input", "r") as f: - data = f.readlines() - - acc = 0 - data = grouper(data, 3) +def process(group: list[str, str, str]) -> int: + x, y, z = group + c = set(x.strip()).intersection(y.strip()).intersection(z.strip()).pop() + return ord(c) - ord('a') + 1 if c >= 'a' else ord(c) - ord('A') + 27 - for group in data: - for c in group[0]: - if group[1].find(c) != -1 and group[2].find(c) != -1: - if 'a' <= c <= 'z': - acc += ord(c) - ord('a') + 1 - else: - acc += ord(c) - ord('A') + 27 - break - print(acc) +def main() -> None: + with open("input", "r") as f: + print(sum(process(group) for group in grouper(f.readlines(), 3))) if __name__ == "__main__": -- cgit v1.2.3