From f8a88e4220ba52cb90cb555dc7b8dac2e94aedd1 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 7 Dec 2023 12:32:09 +0100 Subject: [PATCH] opti --- 2023/day07/src/part1.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/2023/day07/src/part1.py b/2023/day07/src/part1.py index 368e234..f7e4497 100644 --- a/2023/day07/src/part1.py +++ b/2023/day07/src/part1.py @@ -1,4 +1,4 @@ -text: str = open("input.txt", "r").read() +text: str = open("example.txt", "r").read() _value: int = 0 @@ -7,7 +7,7 @@ lines = text.splitlines() CARDS = "AKQJT98765432" def get_same(string: str): - sames = {"original": string} + sames = {"key": string} for card in CARDS: if string.count(card) != 0: bozo: list = sames.get(string.count(card), "") @@ -26,22 +26,22 @@ for line in lines: def get_highest(cards1, cards2, i): if (cards1 == cards2): return 0 - if (i != 1 and len(cards2) > len(cards1)): - return 2 - if (i != 1 and len(cards1) > len(cards2)): - return 1 + 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 - if (CARDS.index(char1) > CARDS.index(char2)): - return 2 - else: - return 1 + return 1 + (CARDS.index(char1) > CARDS.index(char2)) return 0 -def get_index(lst: [dict], original: str): +def get_index(lst: [dict], key: str): for i, hand in enumerate(lst): - if (hand.get("original") == original): + if (hand.get("key") == key): return i sort = [sames[0]] @@ -57,7 +57,8 @@ for same in sames[1:]: if (highest == 0): new.append(opponent) elif (highest == 1): - places.append(get_index(sort, opponent.get("original"))) + places.append(get_index(sort, opponent.get("key"))) + else: opponents = new if (places != []): break @@ -69,9 +70,10 @@ for same in sames[1:]: for i, line in enumerate(lines): key, value = line.split() for j, bozo in enumerate(sort): - if (bozo.get("original", "") == key): - _value += (len(lines) - j) * bozo.get("value") - print(f'{(len(lines) - j)}, {bozo.get("value")}') + if (bozo.get("key", "") == key): + rank: int = (len(lines) - j) + _value += rank * bozo.get("value") + print(f'{rank}, {bozo.get("key")} {bozo.get("value")}') break print(_value) \ No newline at end of file