recreation of part1

This commit is contained in:
starnakin 2023-12-07 17:07:46 +01:00
parent dfa1a49c7b
commit a8ff3fc882

View File

@ -6,68 +6,65 @@ lines = text.splitlines()
CARDS = "AKQJT98765432" CARDS = "AKQJT98765432"
def get_same(string: str): def parse_line(line: str):
same = {"key": string} bozo = line.split()
for card in CARDS: card = bozo[0]
if string.count(card) != 0: bid = int(bozo[1])
bozo: list = same.get(string.count(card), "") same = {"card": card, "bid": bid}
bozo += (card) bozo = set(card)
same.update({string.count(card): bozo}) 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 return same
sames = [] def parse(lines):
hands = []
for line in lines: for line in lines:
bozo: [str] = line.split() hands.append(parse_line(line))
same = get_same(bozo[0]) return hands
same.update({"value": int(bozo[1])})
sames.append(same)
#[print(same) for same in sames] def hand_cmp(hand1: dict, hand2: dict):
for card1, card2 in zip(hand1.get("card"), hand2.get("card")):
def get_highest(cards1, cards2, i): if card1 == card2:
if (cards1 == cards2):
return 0
if (i != 1):
l1 = len(cards1)
l2 = len(cards2)
if (l1 > l2):
return 1
if (l2 > l1):
return 2
for char1, char2 in zip(cards1, cards2):
if (char2 == char1):
continue continue
return 1 + (CARDS.index(char1) > CARDS.index(char2)) return (CARDS.index(card1) - CARDS.index(card2))
return 0
def get_index(lst: [dict], key: str): def get_insert_index(lst: [dict], hand: dict):
for i, hand in enumerate(lst): index: int = None
if (hand.get("key") == key): for i, hand2 in enumerate(lst):
if (hand.get("type") == hand2.get("type")):
if (hand_cmp(hand, hand2) < 0):
return i return i
elif (hand.get("type") > hand2.get("type")):
return i
return len(lst)
sort = [sames[0]] def sort_hands(hands: [dict]):
for same in sames[1:]: sort = [hands[0]]
place = None for hand in hands[1:]:
opponents: [dict] = sort.copy() index = get_insert_index(sort, hand)
for card_index in range(5, 0, -1): sort.insert(index, hand)
new = [] return sort
for opponent in opponents:
my_cards = same.get(card_index, "")
opponent_cards = opponent.get(card_index, "")
highest = get_highest(my_cards, opponent_cards, card_index)
if (highest == 0):
new.append(opponent)
elif (highest == 1):
index = get_index(sort, opponent.get("key"))
place = index if place == None else min(place, index)
opponents = new
if (place == None):
place = len(sort)
sort.insert(place, same)
for j, bozo in enumerate(sort): hands = parse(lines)
rank: int = (len(lines) - j) hands = sort_hands(hands)
_value += rank * bozo.get("value")
print(f'{rank} {bozo.get("key")} {bozo.get("value")}') 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) print(_value)