#!/usr/bin/env python3 from collections import Counter lines = (x.strip() for x in open("input.txt")) hands = [] for line in lines: hand, bid = line.split(" ") hand = tuple("23456789TJQKA".index(char) for char in hand) count = Counter(hand).values() if 5 in count: hands.append((6, hand, int(bid))) elif 4 in count: hands.append((5, hand, int(bid))) elif 3 in count and 2 in count: hands.append((4, hand, int(bid))) elif 3 in count: hands.append((3, hand, int(bid))) elif list(count).count(2) == 2: hands.append((2, hand, int(bid))) elif 2 in count: hands.append((1, hand, int(bid))) else: hands.append((0, hand, int(bid))) hands.sort() print(sum(bid * rank for rank, (_, _, bid) in enumerate(hands, 1)))