From b6f3782b5187a765cafe7596a9572765b165e0c9 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 7 Dec 2023 09:43:34 +0100 Subject: [PATCH] part1 with min value add to index 0 omegacringe --- 2023/day07/example.txt | 7 ++++ 2023/day07/src/part1.py | 76 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 2023/day07/example.txt create mode 100644 2023/day07/src/part1.py diff --git a/2023/day07/example.txt b/2023/day07/example.txt new file mode 100644 index 0000000..6c3501d --- /dev/null +++ b/2023/day07/example.txt @@ -0,0 +1,7 @@ +32T3K 765 +T55J5 684 +KTJJT 220 +QQQJA 483 +77777 1 +KK677 28 +12345 2 \ No newline at end of file diff --git a/2023/day07/src/part1.py b/2023/day07/src/part1.py new file mode 100644 index 0000000..b629ba4 --- /dev/null +++ b/2023/day07/src/part1.py @@ -0,0 +1,76 @@ +text: str = open("example.txt", "r").read() + +_value: int = 0 + +lines = text.splitlines() + +CARDS = "AKQJT98765432" + +def get_same(string: str): + sames = {"original": string} + for card in CARDS: + if string.count(card) != 0: + bozo: list = sames.get(string.count(card), []) + bozo.append(card) + sames.update({string.count(card): bozo}) + return sames + +sames = [] +for line in lines: + bozo: [str] = line.split() + same = get_same(bozo[0]) + sames.append(same) + + +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 + for k in range(min(len(cards2), len(cards1))): + if (cards2[k] == cards1[k]): + continue + if (CARDS.index(cards1[k]) > CARDS.index(cards2[k])): + return 2 + else: + return 1 + return 0 + +sort = [sames[0]] +for same in sames[1:]: + place = None + for i in range(5, 0, -1): + cards = same.get(i) + if (cards == None): + continue + for j, bozo in enumerate(sort): + _cards = bozo.get(i) + if _cards is None: + continue + highest = get_highest(_cards, cards, i) + if (highest == 0 or highest == 1): + continue + else: + place = j + break + if (place is None): + place = 0 + break + if (place is not None): + break + if (place is None): + print(same, i) + place = 0 + sort.insert(place, same) + +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) * int(value) + print( (len(lines) - j), bozo.get("original")) + break + +print(_value) \ No newline at end of file