Files
AdventOfCode2023/07/07-1.py
2023-12-07 15:55:52 +01:00

99 lines
2.7 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(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)