summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-12-09 19:10:21 +0100
committerlonkaars <loek@pipeframe.xyz>2023-12-09 19:10:21 +0100
commitbe634a34fc970139f6e9facb6f3af748a97ca535 (patch)
tree2fb6ca5185422b081d6c449bbddf958d71834008
parent08a11d4a608714e9fe56f899bc9f07003ee237be (diff)
day 7 part 1
-rwxr-xr-x07/main.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/07/main.py b/07/main.py
new file mode 100755
index 0000000..8b525d3
--- /dev/null
+++ b/07/main.py
@@ -0,0 +1,78 @@
+#!/bin/python3
+import sys
+from math import sqrt
+
+output = 0
+
+CARD_MAP = {
+ "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,
+}
+
+FIVE_OF_A_KIND = 7
+FOUR_OF_A_KIND = 6
+FULL_HOUSE = 5
+THREE_OF_A_KIND = 4
+TWO_PAIR = 3
+ONE_PAIR = 2
+HIGH_CARD = 1
+
+hands = []
+for line in sys.stdin:
+ line = line.strip()
+ cards, bid = line.split()
+ bid = int(bid)
+
+ # sort_by is a list of values to sort the final hands list by
+ sort_by = []
+ tally = {}
+ for card in cards:
+ code = CARD_MAP[card]
+ tally[code] = tally.get(code, 0) + 1
+ sort_by.append(code)
+
+ # first index in sort_by is the type of hand
+ hand_type = 0
+ if 5 in tally.values():
+ hand_type = FIVE_OF_A_KIND
+ elif 4 in tally.values():
+ hand_type = FOUR_OF_A_KIND
+ elif 3 in tally.values() and 2 in tally.values():
+ hand_type = FULL_HOUSE
+ elif 3 in tally.values() and 2 == len([v for v in tally.values() if v == 1]):
+ hand_type = THREE_OF_A_KIND
+ elif 2 == len([v for v in tally.values() if v == 2]):
+ hand_type = TWO_PAIR
+ elif 1 == len([v for v in tally.values() if v == 2]):
+ hand_type = ONE_PAIR
+ elif 5 == len([v for v in tally.values() if v == 1]):
+ hand_type = HIGH_CARD
+ else:
+ hand_type = 0
+ sort_by.insert(0, hand_type)
+
+ hand = {
+ "bid": bid,
+ "sort": sort_by,
+ }
+ hands.append(hand)
+
+ # print(cards, bid, tally)
+
+hands.sort(key=lambda h: h["sort"])
+
+output = sum(hand["bid"] * (index + 1) for index, hand in enumerate(hands))
+
+print(f">> {output}")
+