diff --git a/2023/7/7_2.py b/2023/7/7_2.py new file mode 100644 index 0000000..f6f025b --- /dev/null +++ b/2023/7/7_2.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +# 2023 - Advent Of Code 7 - part 2 + +# file = 'input_example.txt' +file = 'input.txt' + + +class Hand: + def __init__(self, cards, bet): + self.cards = cards + self.bet = bet + self.dict_cards = {} + + def __repr__(self): + return repr(f'cards: {self.cards}') + + def sort_cards(self): + self.dict_cards = sorted(self.dict_cards.items(), key=lambda x: x[1], reverse=True) + + +# cards_order = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'] +cards_order = ['J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A'] + +# pylint: disable=consider-using-with +input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")] + +hands = [Hand(line.split()[0], line.split()[1]) for line in input_lines] +# print(hands) + +for h in hands: + hand_dict = {k: 0 for k in cards_order} + for card in h.cards: + hand_dict[card] += 1 + h.dict_cards = {k: v for k, v in hand_dict.items() if v != 0} + +for h in hands: + h.sort_cards() + # print(h.dict_cards) + +hands_5 = [h for h in hands if h.dict_cards[0][1] == 5] +hands_5.extend([h for h in hands if h.dict_cards[0][1] == 4 and (('J', 1) in h.dict_cards or ('J', 4) in h.dict_cards)]) +hands_5.extend([h for h in hands if h.dict_cards[0][1] == 3 and ('J', 2) in h.dict_cards]) +hands_5.extend([h for h in hands if ('J', 3) in h.dict_cards and h.dict_cards[1][1] == 2]) +sorted_5 = sorted(hands_5, key=lambda x: [cards_order.index(c) for c in x.cards]) +filtered_hands = [f for f in hands if f not in sorted_5] +hands = filtered_hands + +hands_4 = [h for h in hands if h.dict_cards[0][1] == 4] +hands_4.extend([h for h in hands if h.dict_cards[0][1] == 3 and (('J', 1) in h.dict_cards or ('J', 3) in h.dict_cards)]) +hands_4.extend([h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[1][1] == 2 and ('J', 2) in h.dict_cards]) +sorted_4 = sorted(hands_4, key=lambda x: [cards_order.index(c) for c in x.cards]) +filtered_hands = [f for f in hands if f not in sorted_4] +hands = filtered_hands + +hands_full = [h for h in hands if h.dict_cards[0][1] == 3 and h.dict_cards[1][1] == 2] +hands_full.extend([h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[1][1] == 2 and ('J', 1) in h.dict_cards]) +sorted_full = sorted(hands_full, key=lambda x: [cards_order.index(c) for c in x.cards]) +filtered_hands = [f for f in hands if f not in sorted_full] +hands = filtered_hands + +hands_3 = [h for h in hands if h.dict_cards[0][1] == 3 and h.dict_cards[1][1] != 2] +hands_3.extend([h for h in hands if h.dict_cards[0][1] == 2 and ('J', 1) in h.dict_cards]) +hands_3.extend([h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[0][0] == 'J' and h.dict_cards[1][1] != 2]) +sorted_3 = sorted(hands_3, key=lambda x: [cards_order.index(c) for c in x.cards]) +filtered_hands = [f for f in hands if f not in sorted_3] +hands = filtered_hands + +hands_twopairs = [h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[1][1] == 2] +hands_3.extend([h for h in hands if h.dict_cards[0][1] == 2 and ('J', 1) in h.dict_cards]) +sorted_twopairs = sorted(hands_twopairs, key=lambda x: [cards_order.index(c) for c in x.cards]) +filtered_hands = [f for f in hands if f not in sorted_twopairs] +hands = filtered_hands + +hands_onepair = [h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[1][1] != 2] +hands_onepair.extend([h for h in hands if h.dict_cards[0][1] == 1 and ('J', 1) in h.dict_cards]) +sorted_onepair = sorted(hands_onepair, key=lambda x: [cards_order.index(c) for c in x.cards]) +filtered_onepair = [f for f in hands if f not in sorted_onepair] +hands = filtered_onepair + +hands_high = [h for h in hands if h.dict_cards[0][1] == 1] +sorted_high = sorted(hands_high, key=lambda x: [cards_order.index(c) for c in x.cards]) + +sorted_hands = sorted_high + sorted_onepair + sorted_twopairs + sorted_3 + sorted_full + sorted_4 + sorted_5 + +acc = 0 +for rank, h in enumerate(sorted_hands): + print(f'{rank+1} {h} {h.bet}') + # print((rank + 1) * int(h.bet)) + acc += (rank+1) * int(h.bet) + +print(acc)