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

138 lines
3.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
card_order = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"]
hand_order = ["Five", "Four", "Full", "Three", "TwoPair", "Pair", "HighCard"]
inp = []
def card_sort(e):
return [card_order.index(c) for c in e]
input_file = open("input", "r")
for line in input_file:
line = line.strip().split()
hand = []
for b in line[0]:
hand.append(b)
inp.append([hand, line[1]])
input_file.close()
# for i in inp:
# print(i)
def hand_type(s):
ss = []
for x in s:
ss.append(x)
ss.sort(key=card_sort)
if s[0] == s[1] == s[2] == s[3] == s[4]:
return "Five", s
else:
for c in card_order:
if s.count(c) == 4:
return "Four", s
elif ss.count(c) == 3:
for cc in card_order:
if cc != c and ss.count(cc) == 2:
return "Full", s
return "Three", s
elif ss.count(c) == 2:
for cc in card_order:
if cc != c and ss.count(cc) == 2:
return "TwoPair", s
elif cc != c and ss.count(cc) == 3:
return "Full", s
return "Pair", s
return "High", s
typed_inp = []
for i in inp:
ty = hand_type(i[0])
typed_inp.append([ty[0], ty[1], i[1]])
# for i in typed_inp:
# print(i)
high_cards = []
pair_cards = []
two_pair_cards = []
three_cards = []
full_cards = []
four_cards = []
five_cards = []
hand_dic = {}
for t in typed_inp:
hand = "".join(t[1])
hand_dic[hand] = int(t[2])
if t[0] == "High":
high_cards.append(hand)
elif t[0] == "Pair":
pair_cards.append(hand)
elif t[0] == "TwoPair":
two_pair_cards.append(hand)
elif t[0] == "Three":
three_cards.append(hand)
elif t[0] == "Full":
full_cards.append(hand)
elif t[0] == "Four":
four_cards.append(hand)
elif t[0] == "Five":
five_cards.append(hand)
else:
print("Da ging was schief", t)
five_cards.sort(key=card_sort, reverse=True)
four_cards.sort(key=card_sort, reverse=True)
full_cards.sort(key=card_sort, reverse=True)
three_cards.sort(key=card_sort, reverse=True)
two_pair_cards.sort(key=card_sort, reverse=True)
pair_cards.sort(key=card_sort, reverse=True)
high_cards.sort(key=card_sort, reverse=True)
solution = 0
i = 0
print("High -----------------------------------------")
for card in high_cards:
i += 1
solution += i*hand_dic[card]
print(i, card, hand_dic[card])
print("Pair -----------------------------------------")
for card in pair_cards:
i += 1
solution += i*hand_dic[card]
print(i, card, hand_dic[card])
print("Two Pair -----------------------------------------")
for card in two_pair_cards:
i += 1
solution += i*hand_dic[card]
print(i, card, hand_dic[card])
print("ThreeCards -----------------------------------------")
for card in three_cards:
i += 1
solution += i*hand_dic[card]
print(i, card, hand_dic[card])
print("FullHouse -----------------------------------------")
for card in full_cards:
i += 1
solution += i*hand_dic[card]
print(i, card, hand_dic[card])
print("FourCards -----------------------------------------")
for card in four_cards:
i += 1
solution += i*hand_dic[card]
print(i, card, hand_dic[card])
print("FiveCards -----------------------------------------")
for card in five_cards:
i += 1
solution += i*hand_dic[card]
print(i, card, hand_dic[card])
print()
print(f"Lösung: {solution}")