Untitled

mail@pastecode.io avatar
unknown
python
6 months ago
1.3 kB
2
Indexable
Never
import fileinput
from collections import defaultdict
from functools import cmp_to_key

def hand_to_nums(hand):
    hist = defaultdict(int)
    js = 0
    for c in hand:
        if c == '0':
            js += 1
        else:
            hist[c] += 1
    str_counts = ""
    for cnt in sorted(hist.values(), reverse=True):
        str_counts += str(cnt)
    cnts_transformed = ""
    total_cnt = 0
    for cnt in str_counts:
        cnt = int(cnt)
        if js != 0:
            diff = min(js, 5 - cnt)
            cnt += diff
            js -= diff
        cnts_transformed += str(cnt)
    if js != 0:
        cnts_transformed += str(js)
    return cnts_transformed

def compare(a, b):
    a_num, b_num = hand_to_nums(a[0]), hand_to_nums(b[0])
    if a_num < b_num:
        return -1
    elif b_num < a_num:
        return 1
    else:
        if a[0] < b[0]:
            return -1
        return 1

mapping = {
    'A':'m', 'K':'l', 'Q':'k', 'J':'j', 'T':'i', '9':'h', '8':'g', '7':'f', '6':'e', '5':'d', '4':'c', '3':'b', '2':'a', 'J':'0'
}
translator = str.maketrans(mapping)


hands = []
for line in fileinput.input():
    hand, bid = line.split()
    hand = hand.translate(translator)
    hands.append((hand, int(bid)))
hands = sorted(hands, key=cmp_to_key(compare))
win = 0
for i, hand in enumerate(hands):
    win += (i + 1)*hand[1]
print(win)
Leave a Comment