From 84723ce9e2457fa8aa0bb2790a8c13242e88ec7e Mon Sep 17 00:00:00 2001 From: mhsn Date: Tue, 10 Dec 2024 20:42:35 +0000 Subject: 2024-05 python cleanup --- 2024/05/python/main.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/2024/05/python/main.py b/2024/05/python/main.py index c365cfe..e3a10e2 100644 --- a/2024/05/python/main.py +++ b/2024/05/python/main.py @@ -1,27 +1,25 @@ from fileinput import input from functools import cmp_to_key +from itertools import takewhile -silver = 0 -gold = 0 - -lines = [line.strip() for line in input()] -split = lines.index("") - -ordering, updates = lines[:split], lines[split + 1 :] +inp = map(str.strip, input()) +ordering = set(takewhile(bool, inp)) +us = list(inp) def cmp(x, y): - if f"{y}|{x}" in ordering: - return 1 - return -1 + return (f"{y}|{x}" in ordering) * 2 - 1 # hehe -for update in updates: - raw = update.split(",") - fixed = list(sorted(raw, key=cmp_to_key(cmp))) +silver = 0 +gold = 0 + +for u in us: + pre = u.split(",") + post = list(sorted(pre, key=cmp_to_key(cmp))) - mid = int(fixed[len(fixed) // 2]) - if fixed == raw: + mid = int(post[len(post) // 2]) + if pre == post: silver += mid else: gold += mid -- cgit v1.2.3