70 lines
1.6 KiB
Python
70 lines
1.6 KiB
Python
text: str = open("input.txt", "r").read()
|
|
|
|
_value: int = 0
|
|
|
|
lines = text.splitlines()
|
|
|
|
CARDS = "AKQJT98765432"
|
|
|
|
def parse_line(line: str):
|
|
bozo = line.split()
|
|
card = bozo[0]
|
|
bid = int(bozo[1])
|
|
same = {"card": card, "bid": bid}
|
|
bozo = set(card)
|
|
if (len(bozo) == 5):
|
|
_type = 0
|
|
elif (len(bozo) == 4):
|
|
_type = 1
|
|
elif (len(bozo) == 3 and (card.count(list(bozo)[0]) == 2 or card.count(list(bozo)[1]) == 2)):
|
|
_type = 2
|
|
elif (len(bozo) == 3):
|
|
_type = 3
|
|
elif (len(bozo) == 2 and (card.count(list(bozo)[0]) == 2 or card.count(list(bozo)[1]) == 2)):
|
|
_type = 4
|
|
elif (len(bozo) == 2):
|
|
_type = 5
|
|
elif (len(bozo) == 1):
|
|
_type = 6
|
|
|
|
same.update({"type": int(_type)})
|
|
return same
|
|
|
|
def parse(lines):
|
|
hands = []
|
|
for line in lines:
|
|
hands.append(parse_line(line))
|
|
return hands
|
|
|
|
def hand_cmp(hand1: dict, hand2: dict):
|
|
for card1, card2 in zip(hand1.get("card"), hand2.get("card")):
|
|
if card1 == card2:
|
|
continue
|
|
return (CARDS.index(card1) - CARDS.index(card2))
|
|
|
|
def get_insert_index(lst: [dict], hand: dict):
|
|
index: int = None
|
|
for i, hand2 in enumerate(lst):
|
|
if (hand.get("type") == hand2.get("type")):
|
|
if (hand_cmp(hand, hand2) < 0):
|
|
return i
|
|
elif (hand.get("type") > hand2.get("type")):
|
|
return i
|
|
return len(lst)
|
|
|
|
def sort_hands(hands: [dict]):
|
|
sort = [hands[0]]
|
|
for hand in hands[1:]:
|
|
index = get_insert_index(sort, hand)
|
|
sort.insert(index, hand)
|
|
return sort
|
|
|
|
hands = parse(lines)
|
|
hands = sort_hands(hands)
|
|
|
|
for i, hand in enumerate(hands):
|
|
_value += (len(hands) - i) * hand.get("bid")
|
|
#print(f'{hand.get("card")} {hand.get("bid")}')
|
|
print(f'{len(hands) - i} {hand.get("card")} {hand.get("bid")} type={hand.get("type")}')
|
|
|
|
print(_value) |