Guest User

Day7 Part1

a guest
Dec 7th, 2023
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import os
  2. import copy
  3. FILE_PATH = os.path.dirname(os.path.abspath(__file__))
  4. INPUT_FILE_PATH = os.path.join(FILE_PATH, "../data/input.txt")
  5. TEST_FILE_PATH = os.path.join(FILE_PATH, "../data/test.txt")
  6.  
  7. cardss = {
  8. "A": 14,
  9. "K": 13,
  10. "Q": 12,
  11. "J": 11,
  12. "T": 10,
  13. "9": 9,
  14. "8": 8,
  15. "7": 7,
  16. "6": 6,
  17. "5": 5,
  18. "4": 4,
  19. "3": 3,
  20. "2": 2,
  21. }
  22.  
  23. five = []
  24. four = []
  25. full = []
  26. three = []
  27. two = []
  28. one = []
  29. high = []
  30.  
  31. class Hand:
  32. def __init__(self, cards, bet):
  33. self.cards = cards
  34. self.bet = bet
  35.  
  36. def __str__(self):
  37. return f"{self.cards}({self.bet})"
  38.  
  39. __repr__ = __str__
  40.  
  41. def __lt__(self, other):
  42. if self.cards[0] == other.cards[0]:
  43. if self.cards[1] == other.cards[1]:
  44. if self.cards[2] == other.cards[2]:
  45. if self.cards[3] == other.cards[3]:
  46. return cardss[self.cards[4]] < cardss[other.cards[4]]
  47. else:
  48. return cardss[self.cards[3]] < cardss[other.cards[3]]
  49. else:
  50. return cardss[self.cards[2]] < cardss[other.cards[2]]
  51. else:
  52. return cardss[self.cards[1]] < cardss[other.cards[1]]
  53. else:
  54. return cardss[self.cards[0]] < cardss[other.cards[0]]
  55.  
  56. def checkhand(hand):
  57. localhand = copy.deepcopy(hand)
  58. hand.cards = ''.join(sorted(hand.cards, key=str.lower))
  59. if hand.cards.count(hand.cards[0]) == 5:
  60. five.append(localhand)
  61. elif hand.cards.count(hand.cards[0]) == 4:
  62. four.append(localhand)
  63. elif hand.cards.count(hand.cards[0]) == 3:
  64. hand.cards = hand.cards.replace(hand.cards[0],"")
  65. if hand.cards.count(hand.cards[0]) == 2:
  66. full.append(localhand)
  67. else:
  68. three.append(localhand)
  69. elif hand.cards.count(hand.cards[0]) == 2:
  70. hand.cards = hand.cards.replace(hand.cards[0],"")
  71. if hand.cards.count(hand.cards[0]) == 3:
  72. full.append(localhand)
  73. elif (hand.cards.count(hand.cards[0]) == 2 ) or (hand.cards.count(hand.cards[1]) == 2 ) or (hand.cards.count(hand.cards[2]) == 2 ):
  74. two.append(localhand)
  75. else:
  76. one.append(localhand)
  77. elif hand.cards.count(hand.cards[0]) == 1:
  78. hand.cards = hand.cards.replace(hand.cards[0],"")
  79. if hand.cards.count(hand.cards[0]) == 4:
  80. four.append(localhand)
  81. elif hand.cards.count(hand.cards[0]) == 3:
  82. hand.cards = hand.cards.replace(hand.cards[0],"")
  83. if hand.cards.count(hand.cards[0]) == 2:
  84. full.append(localhand)
  85. else:
  86. three.append(localhand)
  87. elif hand.cards.count(hand.cards[0]) == 2:
  88. hand.cards = hand.cards.replace(hand.cards[0],"")
  89. if hand.cards.count(hand.cards[0]) == 3:
  90. full.append(localhand)
  91. elif (hand.cards.count(hand.cards[0]) == 2 ) or (hand.cards.count(hand.cards[1]) == 2 ):
  92. two.append(localhand)
  93. else:
  94. one.append(localhand)
  95. elif hand.cards.count(hand.cards[0]) == 1:
  96. hand.cards = hand.cards.replace(hand.cards[0],"")
  97. if hand.cards.count(hand.cards[0]) == 3:
  98. three.append(localhand)
  99. elif hand.cards.count(hand.cards[0]) == 2:
  100. two.append(localhand)
  101. elif hand.cards.count(hand.cards[0]) == 1:
  102. hand.cards = hand.cards.replace(hand.cards[0],"")
  103. if hand.cards.count(hand.cards[0]) == 2:
  104. one.append(localhand)
  105. else:
  106. high.append(localhand)
  107.  
  108. def sortlists():
  109. five.sort()
  110. four.sort()
  111. full.sort()
  112. three.sort()
  113. two.sort()
  114. one.sort()
  115. high.sort()
  116.  
  117. def calculateranksum():
  118. rank = 1
  119. ranksum = 0
  120. for element in high:
  121. print("Rank: "+ str(rank) + ";" + str(element))
  122. ranksum += rank * element.bet
  123. rank += 1
  124. for element in one:
  125. print("Rank: "+ str(rank) + ";" + str(element))
  126. ranksum += rank * element.bet
  127. rank += 1
  128. for element in two:
  129. print("Rank: "+ str(rank) + ";" + str(element))
  130. ranksum += rank * element.bet
  131. rank += 1
  132. for element in three:
  133. print("Rank: "+ str(rank) + ";" + str(element))
  134. ranksum += rank * element.bet
  135. rank += 1
  136. for element in full:
  137. print("Rank: "+ str(rank) + ";" + str(element))
  138. ranksum += rank * element.bet
  139. rank += 1
  140. for element in four:
  141. print("Rank: "+ str(rank) + ";" + str(element))
  142. ranksum += rank * element.bet
  143. rank += 1
  144. for element in five:
  145. print("Rank: "+ str(rank) + ";" + str(element))
  146. ranksum += rank * element.bet
  147. rank += 1
  148.  
  149. print(ranksum)
  150.  
  151. def main():
  152. with open(TEST_FILE_PATH,"r") as file:
  153. # with open(INPUT_FILE_PATH,"r") as file:
  154. lines = file.readlines()
  155.  
  156. for line in lines:
  157. line = line.strip().split(" ")
  158. checkhand(Hand(line[0],int(line[1])))
  159.  
  160. sortlists()
  161. calculateranksum()
  162.  
  163.  
  164. if __name__ == '__main__':
  165. main()
Advertisement
Add Comment
Please, Sign In to add comment