-
Notifications
You must be signed in to change notification settings - Fork 0
/
D07.py
187 lines (152 loc) · 5.21 KB
/
D07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'''
# PART ONE
we are playing poker
# Input
The first string in each line is a five-card poker hand, the
second string is a bid.
for example:
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483
Cards are, in descending order or strength: A K Q J T 9 8 7 6 5 4 3 2
Possible hands are, ordered by strength:
five of a kind
four of a kind
full house (three of a kind + a pair)
three of a kind
two pair
one pair
high card
# Puzzle description
we want to rank the hands in the input file by strength (winning hand = highest number), and
then multiply each rank by it's bid. Add those numbers together
to solve the puzzle.
# Approach
- Sort the cards in each hand to categorize them.
- score the hands for sorting by mutliplying the criteria by powers of 10.
- sort the hands.
- calculate the final score of each hand
'''
FIVE = 'five'
FOUR = 'four'
FULL_HOUSE = 'full_house'
THREE = 'three'
TWO_PAIR = 'two_pair'
PAIR = 'pair'
HIGH_CARD = 'high_card'
ranked_types = [FIVE, FOUR, FULL_HOUSE, THREE, TWO_PAIR, PAIR, HIGH_CARD]
ranked_types.reverse()
with open('D07.txt', 'r') as file:
input = [line.split(' ') for line in file.read().splitlines()]
hands = [(list(cards), int(bid), cards) for [cards, bid] in input]
def part1():
CARDS = '23456789TJQKA'
scored_hands = []
for (cards, bid, og_cards) in hands:
cards.sort(key=lambda card: CARDS.find(card))
previous_card = ''
sequence_length = 1
type = ''
for card in cards:
if card == previous_card:
sequence_length += 1
if sequence_length == 2:
if type == '':
type = PAIR
elif type == PAIR:
type = TWO_PAIR
elif type == THREE:
type = FULL_HOUSE
elif sequence_length == 3:
if type == PAIR:
type = THREE
elif type == TWO_PAIR:
type = FULL_HOUSE
elif sequence_length == 4:
type = FOUR
elif sequence_length == 5:
type = FIVE
else:
sequence_length = 1
previous_card = card
if type == '':
type = HIGH_CARD
score = ranked_types.index(type)*100000000000 + CARDS.find(og_cards[0])*100000000 + CARDS.find(og_cards[1])*1000000 + CARDS.find(og_cards[2])*10000 + CARDS.find(og_cards[3])*100 + CARDS.find(og_cards[4])
scored_hands.append((score, bid, og_cards))
scored_hands.sort(key=lambda data: data[0])
print(sum([(i+1)*scored_hands[i][1] for i in range(len(scored_hands))]))
''''
# PART 2
Js are now wild cards that have the lowest point value in ranking
## Approach
same as before, but count the Js and change the hand type based on how many
wild cards are available. Not very elegant but it works.
'''
def part2():
CARDS = 'J23456789TQKA'
scored_hands = []
for (cards, bid, og_cards) in hands:
cards.sort(key=lambda card: CARDS.find(card))
previous_card = ''
sequence_length = 1
type = ''
joker_count = 0
for card in cards:
if card == 'J':
joker_count += 1
elif card == previous_card:
sequence_length += 1
if sequence_length == 2:
if type == '':
type = PAIR
elif type == PAIR:
type = TWO_PAIR
elif type == THREE:
type = FULL_HOUSE
elif sequence_length == 3:
if type == PAIR:
type = THREE
elif type == TWO_PAIR:
type = FULL_HOUSE
elif sequence_length == 4:
type = FOUR
elif sequence_length == 5:
type = FIVE
else:
sequence_length = 1
previous_card = card
if type == '':
type = HIGH_CARD
if joker_count > 3:
type = FIVE
if joker_count == 3:
if type == PAIR:
type = FIVE
else:
type = FOUR
if joker_count == 2:
if type == THREE:
type = FIVE
elif type == PAIR:
type = FOUR
else:
type = THREE
if joker_count == 1:
if type == FOUR:
type = FIVE
elif type == THREE:
type = FOUR
elif type == PAIR:
type = THREE
elif type == TWO_PAIR:
type = FULL_HOUSE
else:
type = PAIR
score = ranked_types.index(type)*100000000000 + CARDS.find(og_cards[0])*100000000 + CARDS.find(og_cards[1])*1000000 + CARDS.find(og_cards[2])*10000 + CARDS.find(og_cards[3])*100 + CARDS.find(og_cards[4])
scored_hands.append((score, bid, og_cards, type))
scored_hands.sort(key=lambda data: data[0])
print(sum([(i+1)*scored_hands[i][1] for i in range(len(scored_hands))]))
part1()
part2()