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

98
07/07-1.py Normal file
View File

@@ -0,0 +1,98 @@
#!/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(e)
input_file = open("input", "r")
for line in input_file:
line = line.strip().split()
hand = []
for b in line[0]:
hand.append(b)
hand.sort(key=card_sort)
inp.append([hand, line[1]])
input_file.close()
# for i in inp:
# print(i)
def hand_type(s):
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 s.count(c) == 3 and (((s[0] == s[1] or s[0] == s[4]) and s[0] != c) or (s[3] == s[4] and s[3] != c)):
res = [c, c, c]
for cs in s:
if cs != c:
res.append(cs)
return "Full", res
elif s.count(c) == 3:
res = [c, c, c]
for cs in s:
if cs != c:
res.append(cs)
return "Three", res
elif s.count(c) == 2:
res = [c, c]
for cc in card_order:
if cc != c and s.count(cc) == 2:
res = [c, c, cc, cc]
for cs in s:
if cs != c and cs != cc:
res.append(cs)
return "TwoPair", res
for cs in s:
if cs != c:
res.append(cs)
return "Pair", res
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 = []
for t in typed_inp:
if t[0] == "High":
high_cards.append(":".join(["".join(t[1]), t[2]]))
elif t[0] == "Pair":
pair_cards.append(":".join(["".join(t[1]), t[2]]))
elif t[0] == "TwoPair":
two_pair_cards.append(":".join(["".join(t[1]), t[2]]))
elif t[0] == "Three":
three_cards.append(":".join(["".join(t[1]), t[2]]))
elif t[0] == "Full":
full_cards.append(":".join(["".join(t[1]), t[2]]))
elif t[0] == "Four":
four_cards.append(":".join(["".join(t[1]), t[2]]))
elif t[0] == "Five":
five_cards.append(":".join(["".join(t[1]), t[2]]))
else:
print("Da ging was schief")
#five_cards.sort(key=card_sort, reverse=True)
for c in five_cards:
print(c)

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)

1000
07/input Normal file

File diff suppressed because it is too large Load Diff

5
07/input-ex Normal file
View File

@@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

11
07/test.py Normal file
View File

@@ -0,0 +1,11 @@
def custom_sort(e):
order = [2, 9, 1, 5, 3, 6] # Die vorgegebene Reihenfolge
return order.index(e)
# Erstelle die zu sortierende Liste
my_list = [9, 2, 5]
# Sortiere die Liste mithilfe der benutzerdefinierten Funktion
my_list.sort(key=custom_sort)
print(my_list)

13
07/test2.py Normal file
View File

@@ -0,0 +1,13 @@
order = ['d', 'b', 'c', 'e', 'a', 'x', 'z', 'y']
# Die zu sortierende Liste von 5 Zeichen langen Strings
my_list = ['abxyz', 'cabcd', 'bacde']
# Definiere die benutzerdefinierte Sortierfunktion, um die Reihenfolge festzulegen
def custom_sort(s):
return [order.index(c) for c in s]
# Sortiere die Liste mithilfe der benutzerdefinierten Sortierfunktion
my_list.sort(key=custom_sort)
print(my_list) # Output: ['bacde', 'cabcd', 'abxyz']