diff --git a/2023/day07/src/part1.py b/2023/day07/src/part1.py index b629ba4..c70d889 100644 --- a/2023/day07/src/part1.py +++ b/2023/day07/src/part1.py @@ -1,4 +1,4 @@ -text: str = open("example.txt", "r").read() +text: str = open("input.txt", "r").read() _value: int = 0 @@ -10,8 +10,8 @@ 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) + bozo: list = sames.get(string.count(card), "") + bozo += (card) sames.update({string.count(card): bozo}) return sames @@ -38,31 +38,31 @@ def get_highest(cards1, cards2, i): return 1 return 0 +def get_index(lst: [dict], original: str): + for i, hand in enumerate(lst): + if (hand.get("original") == original): + return i + 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): + places = [] + 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): + places.append(get_index(sort, opponent.get("original"))) + opponents = new + if (places != []): break - if (place is None): - print(same, i) - place = 0 + if (places == []): + places = [len(sort)] + place = min(places) sort.insert(place, same) for i, line in enumerate(lines): @@ -70,7 +70,6 @@ for i, line in enumerate(lines): 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