#!/bin/python3 import sys from math import sqrt output = 0 CARD_MAP = { "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "T": 10, "J": 11, "Q": 12, "K": 13, "A": 14, } FIVE_OF_A_KIND = 7 FOUR_OF_A_KIND = 6 FULL_HOUSE = 5 THREE_OF_A_KIND = 4 TWO_PAIR = 3 ONE_PAIR = 2 HIGH_CARD = 1 hands = [] for line in sys.stdin: line = line.strip() cards, bid = line.split() bid = int(bid) # sort_by is a list of values to sort the final hands list by sort_by = [] tally = {} for card in cards: code = CARD_MAP[card] tally[code] = tally.get(code, 0) + 1 sort_by.append(code) # first index in sort_by is the type of hand hand_type = 0 if 5 in tally.values(): hand_type = FIVE_OF_A_KIND elif 4 in tally.values(): hand_type = FOUR_OF_A_KIND elif 3 in tally.values() and 2 in tally.values(): hand_type = FULL_HOUSE elif 3 in tally.values() and 2 == len([v for v in tally.values() if v == 1]): hand_type = THREE_OF_A_KIND elif 2 == len([v for v in tally.values() if v == 2]): hand_type = TWO_PAIR elif 1 == len([v for v in tally.values() if v == 2]): hand_type = ONE_PAIR elif 5 == len([v for v in tally.values() if v == 1]): hand_type = HIGH_CARD else: hand_type = 0 sort_by.insert(0, hand_type) hand = { "bid": bid, "sort": sort_by, } hands.append(hand) # print(cards, bid, tally) hands.sort(key=lambda h: h["sort"]) output = sum(hand["bid"] * (index + 1) for index, hand in enumerate(hands)) print(f">> {output}")