diff options
author | mhsn <mail@mhsn.net> | 2024-12-05 12:00:00 +0000 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2024-12-05 12:00:00 +0000 |
commit | 71aa0ee1fdbbc07a730346e46872681b26671287 (patch) | |
tree | 6b24c344427a3f109314bb44d4ec2ba562a7fe3a | |
parent | 9a0c0d1ac9d8429ae152124037a1287caefd1aa5 (diff) | |
download | aoc-71aa0ee1fdbbc07a730346e46872681b26671287.tar.gz aoc-71aa0ee1fdbbc07a730346e46872681b26671287.zip |
2024-05 python p1,p2
-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) |