text: str = open("input.txt", "r").read() _value: int = 0 lines = text.splitlines() CARDS = "AKQJT98765432" def get_same(string: str): same = {"key": string} for card in CARDS: if string.count(card) != 0: bozo: list = same.get(string.count(card), "") bozo += (card) same.update({string.count(card): bozo}) return same sames = [] for line in lines: bozo: [str] = line.split() same = get_same(bozo[0]) same.update({"value": int(bozo[1])}) sames.append(same) #[print(same) for same in sames] def get_highest(cards1, cards2, i): if (cards1 == cards2): return 0 if (i != 1): l1 = len(cards1) l2 = len(cards2) if (l1 > l2): return 1 if (l2 > l1): return 2 for char1, char2 in zip(cards1, cards2): if (char2 == char1): continue return 1 + (CARDS.index(char1) > CARDS.index(char2)) return 0 def get_index(lst: [dict], key: str): for i, hand in enumerate(lst): if (hand.get("key") == key): return i sort = [sames[0]] for same in sames[1:]: place = None opponents: [dict] = sort.copy() for card_index in range(5, 0, -1): new = [] for opponent in opponents: my_cards = same.get(card_index, "") opponent_cards = opponent.get(card_index, "") highest = get_highest(my_cards, opponent_cards, card_index) if (highest == 0): new.append(opponent) elif (highest == 1): index = get_index(sort, opponent.get("key")) place = index if place == None else min(place, index) opponents = new if (place == None): place = len(sort) sort.insert(place, same) for j, bozo in enumerate(sort): rank: int = (len(lines) - j) _value += rank * bozo.get("value") print(f'{rank} {bozo.get("key")} {bozo.get("value")}') print(_value)