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"
def get_same(string: str):
same = {"key": string}
for card in CARDS:
if string.count(card) != 0:
bozo: list = same.get(string.count(card), "")
bozo += (card)
same.update({string.count(card): bozo})
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
sames = []
for line in lines:
bozo: [str] = line.split()
same = get_same(bozo[0])
same.update({"value": int(bozo[1])})
sames.append(same)
def parse(lines):
hands = []
for line in lines:
hands.append(parse_line(line))
return hands
#[print(same) for same in sames]
def get_highest(cards1, cards2, i):
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):
def hand_cmp(hand1: dict, hand2: dict):
for card1, card2 in zip(hand1.get("card"), hand2.get("card")):
if card1 == card2:
continue
return 1 + (CARDS.index(char1) > CARDS.index(char2))
return 0
return (CARDS.index(card1) - CARDS.index(card2))
def get_index(lst: [dict], key: str):
for i, hand in enumerate(lst):
if (hand.get("key") == key):
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)
sort = [sames[0]]
for same in sames[1:]:
place = None
opponents: [dict] = sort.copy()
for card_index in range(5, 0, -1):
new = []
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)
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
for j, bozo in enumerate(sort):
rank: int = (len(lines) - j)
_value += rank * bozo.get("value")
print(f'{rank} {bozo.get("key")} {bozo.get("value")}')
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)