This commit is contained in:
starnakin 2023-12-07 12:32:09 +01:00
parent c9a3d978d8
commit f8a88e4220

View File

@ -1,4 +1,4 @@
text: str = open("input.txt", "r").read() text: str = open("example.txt", "r").read()
_value: int = 0 _value: int = 0
@ -7,7 +7,7 @@ lines = text.splitlines()
CARDS = "AKQJT98765432" CARDS = "AKQJT98765432"
def get_same(string: str): def get_same(string: str):
sames = {"original": string} sames = {"key": string}
for card in CARDS: for card in CARDS:
if string.count(card) != 0: if string.count(card) != 0:
bozo: list = sames.get(string.count(card), "") bozo: list = sames.get(string.count(card), "")
@ -26,22 +26,22 @@ for line in lines:
def get_highest(cards1, cards2, i): def get_highest(cards1, cards2, i):
if (cards1 == cards2): if (cards1 == cards2):
return 0 return 0
if (i != 1 and len(cards2) > len(cards1)): if (i != 1):
return 2 l1 = len(cards1)
if (i != 1 and len(cards1) > len(cards2)): l2 = len(cards2)
return 1 if (l1 > l2):
return 1
if (l2 > l1):
return 2
for char1, char2 in zip(cards1, cards2): for char1, char2 in zip(cards1, cards2):
if (char2 == char1): if (char2 == char1):
continue continue
if (CARDS.index(char1) > CARDS.index(char2)): return 1 + (CARDS.index(char1) > CARDS.index(char2))
return 2
else:
return 1
return 0 return 0
def get_index(lst: [dict], original: str): def get_index(lst: [dict], key: str):
for i, hand in enumerate(lst): for i, hand in enumerate(lst):
if (hand.get("original") == original): if (hand.get("key") == key):
return i return i
sort = [sames[0]] sort = [sames[0]]
@ -57,7 +57,8 @@ for same in sames[1:]:
if (highest == 0): if (highest == 0):
new.append(opponent) new.append(opponent)
elif (highest == 1): elif (highest == 1):
places.append(get_index(sort, opponent.get("original"))) places.append(get_index(sort, opponent.get("key")))
else:
opponents = new opponents = new
if (places != []): if (places != []):
break break
@ -69,9 +70,10 @@ for same in sames[1:]:
for i, line in enumerate(lines): for i, line in enumerate(lines):
key, value = line.split() key, value = line.split()
for j, bozo in enumerate(sort): for j, bozo in enumerate(sort):
if (bozo.get("original", "") == key): if (bozo.get("key", "") == key):
_value += (len(lines) - j) * bozo.get("value") rank: int = (len(lines) - j)
print(f'{(len(lines) - j)}, {bozo.get("value")}') _value += rank * bozo.get("value")
print(f'{rank}, {bozo.get("key")} {bozo.get("value")}')
break break
print(_value) print(_value)