07-1 noch Fehler

This commit is contained in:
2023-12-07 15:55:52 +01:00
parent 187d694dc3
commit 51fa3d4c41
6 changed files with 1249 additions and 0 deletions

122
07/07-1a.py Normal file
View File

@@ -0,0 +1,122 @@
#!/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 and (((ss[0] == ss[1] or ss[0] == ss[4]) and ss[0] != c) or (ss[3] == s[4] and ss[3] != c)):
return "Full", s
elif ss.count(c) == 3:
return "Three", s
elif ss.count(c) == 2:
for cc in card_order:
if cc != c and ss.count(cc) == 2:
return "TwoPair", s
return "Pair", s
else:
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)
#for c in high_cards:
# print(c)
solution = 0
i = 0
for card in high_cards:
i += 1
solution += i*hand_dic[card]
for card in pair_cards:
i += 1
solution += i*hand_dic[card]
for card in two_pair_cards:
i += 1
solution += i*hand_dic[card]
for card in three_cards:
i += 1
solution += i*hand_dic[card]
for card in full_cards:
i += 1
solution += i*hand_dic[card]
for card in four_cards:
i += 1
solution += i*hand_dic[card]
for card in five_cards:
i += 1
solution += i*hand_dic[card]
print(solution, i)