Skip to content

Commit

Permalink
parsing hands
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGinnane committed Jun 15, 2024
1 parent 5a0140d commit 1a7b0df
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions adventofcode/2023/7/aoc_2023_07.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,50 @@ def padr(s, l):
return left(str(s) + " " * l, l)

class hand:
def __init__(self, original):
self.original_string = original
card_hierarchy = {
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"T": 10,
"J": 11,
"Q": 12,
"K": 13,
"A": 14
}

def __init__(self, hand_str:str):
self.hand_str = hand_str.strip()

cards_str, bid_str = re.search("([A-Z0-9]{5}) ([0-9]+)", self.hand_str).group(1, 2)
self.cards_str = cards_str
self.bid = int(bid_str)

self.cards = []
self.summary = {}

for c in cards_str:
self.cards.append(c)
if not c in self.summary:
self.summary[c] = 1
else:
self.summary[c] += 1

self.cards.sort(key=lambda x: hand.card_hierarchy[x], reverse=True)
self.summary = dict(sorted(self.summary.items(), key=lambda item: item[1], reverse=True))

def __str__(self):
return self.original_string
return "Cards: " + "".join(self.cards) + ", Bid: " + pad(self.bid, 5) + ", Summary: " + ", ".join([str(v)+"x"+k for (k, v) in self.summary.items()])

lines = open("test_07.txt", "r").readlines()
hands = []

for line in lines:
hands.append(hand(line.strip()))
hands.append(hand(line))

for h in hands:
print(h)

0 comments on commit 1a7b0df

Please sign in to comment.