Files
AdventOfCode2023/07/AOCDay7-Eric.py
2023-12-07 19:49:33 +01:00

108 lines
3.2 KiB
Python

file = open("input", "r")
bids = []
hands = []
rating = {"A": 13, "K": 12, "Q": 11, "J": 10, "T": 9, "9": 8, "8": 7, "7": 6, "6": 5, "5": 4, "4": 3, "3": 2, "2": 1}
for line in file:
count_dict = {}
line = line.strip().split()
bids.append("")
bids.append(line[1].strip())
hands.append(line[0])
for char in line[0]:
if count_dict.get(char):
count_dict[char] += 1
else:
count_dict[char] = 1
hands.append(count_dict)
five_of = []
four_of = []
three_of = []
two_pair = []
one_pair = []
high_card = []
full_house = []
for i in range(len(hands)):
hand = hands[i]
if type(hand) is dict:
if len(hand) == 1:
five_of.append(hands[i - 1])
five_of.append(hand)
five_of.append(bids[i])
continue
elif len(hand) == 4:
one_pair.append(hands[i - 1])
one_pair.append(hand)
one_pair.append(bids[i])
continue
elif len(hand) == 5:
high_card.append(hands[i - 1])
high_card.append(hand)
high_card.append(bids[i])
continue
for j in hand:
if (len(hand) == 2) and (hand[j] == 2 or hand[j] == 3):
full_house.append(hands[i - 1])
full_house.append(hand)
full_house.append(bids[i])
break
elif hand[j] == 4:
four_of.append(hands[i - 1])
four_of.append(hand)
four_of.append(bids[i])
break
elif len(hand) == 3 and hand[j] == 3:
three_of.append(hands[i - 1])
three_of.append(hand)
three_of.append(bids[i])
break
elif len(hand) == 3 and hand[j] == 2:
two_pair.append(hands[i - 1])
two_pair.append(hand)
two_pair.append(bids[i])
break
all_hands = [five_of, four_of, full_house, three_of, two_pair, one_pair, high_card]
specific_rate = len(bids) // 2
blaetter = []
hand_ratings = []
order = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2", "1"]
my_list = ["KTJJT", "KK677"]
def custom_sort(s):
return [order.index(c) for c in s]
for hand in all_hands:
for i in range(1, len(hand)):
single_hand = hand[i]
val = 0
if type(single_hand) is dict:
if len(hand) > 3:
blaetter = []
for j in range(0, len(hand), 3):
blaetter.append(hand[j])
blaetter.sort(key=custom_sort)
else:
hand_ratings.append(specific_rate)
specific_rate -= 1
if i + 1 < len(hand):
hand_ratings.append(hand[i + 1])
continue
for i in range(len(blaetter)):
idx = hand.index(blaetter[i]) + 2
hand_ratings.append(specific_rate)
hand_ratings.append(hand[idx])
specific_rate -= 1
blaetter = []
werte = []
result = 0
for i in range(0, len(hand_ratings), 2):
print(hand_ratings[i], hand_ratings[i + 1])
result += int(hand_ratings[i]) * int(hand_ratings[i + 1])
print(result)