from collections import (
    defaultdict,
    namedtuple,
)
from path import dirpath


def _value(rank):
    try:
        return int(rank)
    except ValueError:
        return 10 + 'TJQKA'.index(rank)


def _sort_by_rank(hand):
    return list(reversed(sorted(
        hand,
        key=lambda card: _value(card[0]),
    )))

    
def _of_a_kind(hand, count):
    counts = defaultdict(list)
    for card in hand:
        counts[card[0]].append(card)
    filtered = {
        rank: cards for
        rank, cards in counts.items() if
        count <= len(cards)
    }
    if len(filtered) < 1:
        return None
    return max(
        filtered.values(),
        key=lambda cards: _value(cards[0][0])
    )


def high_card(hand):
    return _of_a_kind(hand, 1)


def two_of_a_kind(hand):
    return _of_a_kind(hand, 2)


def three_of_a_kind(hand):
    return _of_a_kind(hand, 3)


def four_of_a_kind(hand):
    return _of_a_kind(hand, 4)


def full_house(hand):
    three = three_of_a_kind(hand)
    if not three:
        return None
    pair = two_of_a_kind([card for card in hand if card not in three])
    if not pair:
        return None
    return three + pair


def straight(hand):
    sorted_ = sorted([_value(card[0]) for card in hand])
    if sorted_ == list(range(sorted_[0], sorted_[-1] + 1)):
        return _sort_by_rank(hand)
    return None


def flush(hand):
    counts = defaultdict(list)
    for card in hand:
        counts[card[1]].append(card)
    for cards in counts.values():
        if len(cards) == 5:
            return _sort_by_rank(cards)
    return None


def straight_flush(hand):
    return flush(hand) if straight(hand) else None


def ans():
    lines = open(dirpath() + '054.txt').readlines()
    turns = [line.strip().split() for line in lines]
    num_wins = 0
    for cards in turns:
        one = cards[:5]
        two = cards[5:]
    
    return None
    

if __name__ == '__main__':
    print(ans())

@@ -700,24 +700,353 @@
 0%5D)%0A    )%0A%0A%0A
+def _multi_of_a_kind(hand, first_grouping_func):%0A    first_group = first_grouping_func(hand)%0A    if not first_group:%0A        return None%0A    second_group = two_of_a_kind(%5B%0A        card for card in hand if%0A        card not in first_group%0A    %5D)%0A    if not second_group:%0A        return None%0A    return first_group + second_group%0A%0A%0A
 def high_car
@@ -1272,229 +1272,147 @@
 def 
-full_house(hand):%0A    three = three_of_a_kind(hand)%0A    if not three:%0A        return None%0A    pair = two_of_a_kind(%5Bcard for card in hand if card not in three%5D)%0A    if not pair:%0A   
+two_pair(hand):%0A    return _multi_of_a_kind(hand, two_of_a_kind)%0A%0A%0Adef full_house(hand):%0A
     
- 
 return 
-None%0A    return three + pair
+_multi_of_a_kind(hand, three_of_a_kind)
 %0A%0A%0Ad
@@ -1902,24 +1902,987 @@
 else None%0A%0A%0A
+def list_less_than(list_one, list_two):%0A    for i in range(len(list_one)):%0A        first = list_one%5Bi%5D%0A        second = list_two%5Bi%5D%0A        if _value(first%5B0%5D) %3C _value(second%5B0%5D):%0A            return True%0A    return False%0A%0A%0Adef one_wins(one, two):%0A    for func in %5B%0A        straight_flush,    %0A        four_of_a_kind,    %0A        full_house,%0A        flush,%0A        straight,%0A        three_of_a_kind,%0A        two_pair,%0A        two_of_a_kind,%0A        high_card,%0A    %5D:%0A        list_one = func(one)%0A        list_two = func(two)%0A        if list_one:%0A            if not list_two:%0A                return True%0A            if list_less_than(list_one, list_two):%0A                return False%0A            if list_less_than(list_two, list_one):%0A                return True%0A            one = %5Bc for c in one if c not in list_one%5D%0A            two = %5Bc for c in two if c not in list_two%5D%0A        elif list_two:%0A            return False%0A    raise Exception('No clear winner')%0A%0A%0A
 def ans():%0A 
@@ -2993,16 +2993,20 @@
     num_
+one_
 wins = 0
@@ -3082,32 +3082,96 @@
 5:%5D%0A    
-%0A    return None
+    if one_wins(one, two):%0A            num_one_wins += 1%0A    return num_one_wins
 %0A    %0A%0Ai

Solve 54