-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-7.1.py
40 lines (28 loc) · 1.06 KB
/
day-7.1.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
import re
from collections import Counter
from functools import cmp_to_key
with open("input.txt",encoding="utf-8") as inpf:
data = inpf.read().split("\n")
cards_rank = { "A" : 13, "K" : 12, "Q" : 11, "J" : 10, "T" : 9, "9" : 8, "8" : 7, "7" : 6, "6" : 5, "5" : 4, "4" : 3, "3" : 2, "2" : 1}
hands_rank = {"5" : 7, "14" : 6, "23" : 5, "113" : 4, "122" : 3, "1112" : 2, "11111" : 1}
card_bets = [{"card":x.split()[0].strip(),"bet":int(x.split()[1].strip())} for x in data]
def get_cards_rank(card):
return [cards_rank[x] for x in card["card"]]
def get_hand_rank(card):
return hands_rank["".join(map(str,sorted(list(Counter(card["card"]).values()))))]
def card_sorter(a,b):
a_rank = get_hand_rank(a)
b_rank = get_hand_rank(b)
if a_rank != b_rank:
return a_rank - b_rank
a_rank = get_cards_rank(a)
b_rank = get_cards_rank(b)
while a_rank[0] == b_rank[0]:
a_rank.pop(0)
b_rank.pop(0)
return a_rank[0] - b_rank[0]
card_bets.sort(key=cmp_to_key(card_sorter))
answer = 0
for i,c in enumerate(card_bets):
answer += (i+1)*c["bet"]
print(answer)