From 71aa0ee1fdbbc07a730346e46872681b26671287 Mon Sep 17 00:00:00 2001 From: mhsn Date: Thu, 5 Dec 2024 12:00:00 +0000 Subject: 2024-05 python p1,p2 --- 2024/05/python/main.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2024/05/python/main.py diff --git a/2024/05/python/main.py b/2024/05/python/main.py new file mode 100644 index 0000000..c365cfe --- /dev/null +++ b/2024/05/python/main.py @@ -0,0 +1,30 @@ +from fileinput import input +from functools import cmp_to_key + +silver = 0 +gold = 0 + +lines = [line.strip() for line in input()] +split = lines.index("") + +ordering, updates = lines[:split], lines[split + 1 :] + + +def cmp(x, y): + if f"{y}|{x}" in ordering: + return 1 + return -1 + + +for update in updates: + raw = update.split(",") + fixed = list(sorted(raw, key=cmp_to_key(cmp))) + + mid = int(fixed[len(fixed) // 2]) + if fixed == raw: + silver += mid + else: + gold += mid + +print("silver:", silver) +print("gold:", gold) -- cgit v1.2.3