Skip to content

Instantly share code, notes, and snippets.

@thisiswei
Created February 8, 2013 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thisiswei/4736306 to your computer and use it in GitHub Desktop.
Save thisiswei/4736306 to your computer and use it in GitHub Desktop.
files = file('54.txt').readlines()
hands = [h.replace('\n','') for h in files]
def result():
dicts = {0:0, 1:0}
for i in range(1000):
h1, h2 = split_hand(hands[i])
dicts[who_won(h1,h2)] +=1
return dicts
def split_hand(h):
""" '8C TS KC 9H 4S 7D 2S 5D 3S AC'
=> ['8C', 'TS', 'KC', '9H', '4S']
['7D', '2S', '5D', '3S', 'AC']
"""
h1 = h.split()
return [h1[i] for i in range(5)], [h1[i] for i in range(5,10)]
def who_won(h1,h2):
return h2 == max(h1,h2,key=hand_rank)
def group(rank):
""" [7,5,4,3,5] => [(2, 5), (1, 7), (1, 4), (1, 3)] """
return sorted([(rank.count(r),r) for r in set(rank) ],reverse = True)
def unzip(group):
return zip(*group)
def hand_rank(hand):
""" ['8C', 'TS', 'KC', '9H', '4S'] """
groups = group(['..23456789TJQKA'.index(r) for r,s in hand])
count, ranks = unzip(groups)
if ranks == (14, 5, 4, 3, 2):
ranks = (5, 4, 3, 2, 1)
points = {(4,1):7,
(3,2):6,
(3,1,1):3,
(2,2,1):2,
(2,1,1,1):1,
(1,1,1,1,1):0
}
straight = (max(ranks) - min(ranks) == 4 and len(set(ranks)) == 5)
flush = len(set([s for r,s in hand])) == 1
return max(points[count], 5*flush+4*straight),ranks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment