2023 day 7 part 2 solved

kwout 11 months ago
parent 447cc032f2
commit 26d54e9557
  1. 42
      2023/7.py

@ -1,55 +1,67 @@
hands = [l.split() for l in open("input.txt", 'r').read().splitlines()]
sort1 = [[] for i in range(7)]
sort2 = [[] for i in range(7)]
for hand in hands:
handSet = {}
for card in set(hand[0]):
handSet[card] = hand[0].count(card)
handSetLen = len(handSet)
jCount = hand[0].count('J')
if handSetLen == 1:
sort1[0].append(hand) # 5oak
sort2[0].append(hand)
elif handSetLen == 2:
for card in handSet:
if handSet[card] == 4:
sort1[1].append(hand) # 4oak
sort2[0 if jCount > 0 else 1].append(hand)
break
elif handSet[card] == 3:
sort1[2].append(hand) # fh
sort2[0 if jCount > 0 else 2].append(hand)
break
elif handSetLen == 3:
for card in handSet:
if handSet[card] == 3:
sort1[3].append(hand) # 3oak
sort2[1 if jCount > 0 else 3].append(hand)
break
elif handSet[card] == 2:
sort1[4].append(hand) # 2pair
sort2[(2 if jCount == 1 else 1) if jCount > 0 else 4].append(hand)
break
elif handSetLen == 4:
for card in handSet:
if handSet[card] == 2:
sort1[5].append(hand) # 1pair
sort2[3 if jCount > 0 else 5].append(hand)
break
elif handSetLen == 5:
for card in handSet:
sort1[6].append(hand) # highcard
break
sort2[5 if jCount > 0 else 6].append(hand)
score = 0
scoreIndex = 1
cards = "AKQJT98765432"[::-1]
cards1 = "AKQJT98765432"[::-1]
cards2 = "AKQT98765432J"[::-1]
for i in range(7):
sortTemp = {}
for hand in sort1[-i-1]:
converted = ""
for c in hand[0]:
converted += hex(cards.find(c))[2:]
sortTemp[int('0x' + converted, 16)] = int(hand[1])
for index in sorted(sortTemp):
score += scoreIndex * sortTemp[index]
scoreIndex += 1
def findScore(cards, hands):
score = 0
scoreIndex = 1
for i in range(7):
sortTemp = {}
for hand in hands[-i-1]:
converted = ""
for c in hand[0]:
converted += hex(cards.find(c))[2:]
sortTemp[int('0x' + converted, 16)] = int(hand[1])
print(score)
for index in sorted(sortTemp):
score += scoreIndex * sortTemp[index]
scoreIndex += 1
return score
print(findScore(cards1, sort1))
print(findScore(cards2, sort2))
Loading…
Cancel
Save