-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.py
55 lines (45 loc) · 1.5 KB
/
7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# pylint: skip-file
# mypy: ignore-errors
# flake8: noqa
from collections import Counter
input_value = open("input.txt", "r").read()
lines = input_value.split("\n")
hand_bids = [x.split() for x in lines]
hand_bids = [(x[0], int(x[1])) for x in hand_bids]
# cards = "23456789TJQKA"
cards = "J23456789TQKA"
def score_tiebreak(hand):
total = 0
multiplier = len(cards) ** len(hand)
for card in hand:
total += cards.index(card) * multiplier
multiplier /= len(cards)
return round(total)
def score(hand):
counter = Counter(hand)
# This is the really devilish bit...
count_j = counter["J"]
del counter["J"]
counts = sorted(counter.values(), reverse=True)
# Five of a kind:
if len(counts) == 0 or count_j >= (5 - counts[0]):
return (7, score_tiebreak(hand))
# Four of a kind:
elif count_j >= (4 - counts[0]):
return (6, score_tiebreak(hand))
# Full house:
elif count_j >= (3 - counts[0]) + (2 - counts[1]):
return (5, score_tiebreak(hand))
# Three of a kind:
elif count_j >= (3 - counts[0]):
return (4, score_tiebreak(hand))
# Two pair:
elif count_j >= (2 - counts[0]) + (2 - counts[1]):
return (3, score_tiebreak(hand))
# One pair:
elif count_j >= (2 - counts[0]):
return (2, score_tiebreak(hand))
else:
return (1, score_tiebreak(hand))
hand_bids = sorted(hand_bids, key=lambda hand_bid: score(hand_bid[0]))
print(sum(hand_bids[i][1] * (i + 1) for i in range(len(hand_bids))))