Poker hands

Problem 54

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.
Consider the following five hands dealt to two players:
Hand Player 1 Player 2 Winner
1 5H 5C 6S 7S KD
Pair of Fives
 2C 3S 8S 8D TD
Pair of Eights
 Player 2
2 5D 8C 9S JS AC
Highest card Ace
 2C 5C 7D 8S QH
Highest card Queen
 Player 1
3 2D 9C AS AH AC
Three Aces
 3D 6D 7D TD QD
Flush with Diamonds
 Player 2
4 4D 6S 9H QH QC
Pair of Queens
Highest card Nine
 3D 6D 7H QD QS
Pair of Queens
Highest card Seven
 Player 1
5 2H 2D 4C 4D 4S
Full House
With Three Fours
 3C 3D 3S 9S 9D
Full House
with Three Threes
 Player 1

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.

How many hands does Player 1 win?
from tools import load_data_table
from collections import Counter

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']


def count_cmp(c1, c2):
    if c1[1] < c2[1]:
        return 1
    if c1[1] > c2[1]:
        return -1
    if c1[0] < c2[0]:
        return 1
    if c1[0] > c2[0]:
        return -1
    return 0


def value(hand):
    suits = [card[1] for card in hand]
    values = [ranks.index(card[0]) for card in hand]
    counts = Counter(values).most_common()
    values = [c[0] for c in sorted(counts, count_cmp)]

    flush = suits[1:] == suits[:-1]
    straight = len(values) == 5 and values[0] == values[1] + 1 == values[2] + 2 == values[3] + 3 == values[4] + 4

    if straight and flush:
        return [8] + values
    if counts[0][1] == 4:
        return [7] + values
    if counts[0][1] == 3 and counts[1][1] == 2:
        return [6] + values
    if flush:
        return [5] + values
    if straight:
        return [4] + values
    if counts[0][1] == 3:
        return [3] + values
    if counts[0][1] == 2 and counts[1][1] == 2:
        return [2] + values
    if counts[0][1] == 2:
        return [1] + values
    return [0] + values


def winner(hand1, hand2):
    value1 = value(hand1)
    value2 = value(hand2)
    for i in range(len(value1)):
        if value1[i] > value2[i]:
            return 1
        if value1[i] < value2[i]:
            return 2
    return None


def run():
    count = 0
    cards_list = load_data_table(54)
    for cards in cards_list:
        if winner(cards[:5], cards[5:]) == 1:
            count += 1

    return count