diff options
-rw-r--r-- | 2024/05/python/main.py | 30 |
1 files changed, 30 insertions, 0 deletions
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) |