-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-2.ts
281 lines (261 loc) Β· 9.27 KB
/
7-2.ts
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* --- Part Two ---
To make things a little more interesting, the Elf introduces one additional rule. Now, J cards are jokers - wildcards that can act like whatever card would make the hand the strongest type possible.
To balance this, J cards are now the weakest individual cards, weaker even than 2. The other cards stay in the same order: A, K, Q, T, 9, 8, 7, 6, 5, 4, 3, 2, J.
J cards can pretend to be whatever card is best for the purpose of determining hand type; for example, QJJQ2 is now considered four of a kind. However, for the purpose of breaking ties between two hands of the same type, J is always treated as J, not the card it's pretending to be: JKKK2 is weaker than QQQQ2 because J is weaker than Q.
Now, the above example goes very differently:
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483
32T3K is still the only one pair; it doesn't contain any jokers, so its strength doesn't increase.
KK677 is now the only two pair, making it the second-weakest hand.
T55J5, KTJJT, and QQQJA are now all four of a kind! T55J5 gets rank 3, QQQJA gets rank 4, and KTJJT gets rank 5.
With the new joker rule, the total winnings in this example are 5905.
Using the new joker rule, find the rank of every hand in your set. What are the new total winnings? */
const TEST_HANDS: { [rank: number]: string } = {
1: "32T3K 765",
2: "KK677 28",
3: "T55J5 684",
4: "QQQJA 483",
5: "KTJJT 220",
};
function orderHands(hands: string[]): string[] {
// If the list length is 0 or 1, it's already sorted
if (hands.length <= 1) {
return hands;
}
// Otherwise, split the list into two sublists and sort those.
const midway = Math.floor(hands.length / 2);
const hands1 = orderHands(hands.slice(0, midway));
const hands2 = orderHands(hands.slice(midway));
// Then merge the resultant lists.
let merged: string[] = [];
// While there are still elements in both lists...
while (hands1.length && hands2.length) {
const compare = compareHands(hands1[0]!, hands2[0]!);
// Take the weaker hand and add it to the merged list.
if (compare === -1) {
merged.push(hands1.shift()!);
} else {
merged.push(hands2.shift()!);
}
}
// Now, whichever list still has elements in it can just be tacked onto the end. It's
// already sorted.
merged = merged.concat(hands1.length ? hands1 : hands2);
return merged;
}
// Every possible label of card, ordered from weakest to strongest
const CARD_LABELS = [
"J",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"T",
"Q",
"K",
"A",
];
// Every possible type of hand, ordered from weakest to strongest
const HAND_TYPES = [
"HIGH_CARD",
"ONE_PAIR",
"TWO_PAIR",
"THREE_OF_A_KIND",
"FULL_HOUSE",
"FOUR_OF_A_KIND",
"FIVE_OF_A_KIND",
];
// Helper function which compares the strengths of two hands and returns -1 if the second
// hand is stronger or 1 if the first hand is stronger.
function compareHands(hand1: string, hand2: string): -1 | 1 {
const hand1Type = determineHandType(hand1);
const hand2Type = determineHandType(hand2);
// If the hands are actually the same type...
if (hand1Type === hand2Type) {
// Starting at the beginning, compare each card in order until the first difference.
let index = 0;
while (hand1[index] === hand2[index]) {
index = index + 1;
}
const card1 = hand1[index]!;
const card2 = hand2[index]!;
// The stronger card means the stronger hand.
return CARD_LABELS.indexOf(card1) < CARD_LABELS.indexOf(card2) ? -1 : 1;
}
return HAND_TYPES.indexOf(hand1Type) < HAND_TYPES.indexOf(hand2Type) ? -1 : 1;
}
type GroupSize = 1 | 2 | 3 | 4 | 5;
function determineHandType(handAndBet: string): (typeof HAND_TYPES)[number] {
// Start by stripping the bet and sorting the cards in the hand. This will make it easier to count them.
const cards = handAndBet
.replace(/\s+\d+/, "")
.split("")
.sort();
// We'll now count the number of groups of cards that share the same label, e.g. the
// number of 3-of-a-kinds, pairs..., as well as the number of jokers encountered.
const numberOfGroups: Record<GroupSize, number> = {
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
};
let numberOfJokers = 0;
let index = 0;
let lastCard: string | undefined;
let currentGroupSize: 0 | GroupSize = 0;
while (index < cards.length) {
// If the current card is a joker, just count it and move on.
if (cards[index] === "J") {
numberOfJokers = numberOfJokers + 1;
}
// If the current card is the same label as the last card...
else if (lastCard && lastCard === cards[index]) {
// Increase the current group size.
currentGroupSize = (currentGroupSize + 1) as GroupSize;
} else {
// Otherwise, conclude the last group and record that we found a group of that size
if (currentGroupSize > 0) {
numberOfGroups[currentGroupSize as GroupSize] =
numberOfGroups[currentGroupSize as GroupSize] + 1;
}
// Reset the group size and last card type for the new group that's starting.
currentGroupSize = 1;
lastCard = cards[index];
}
index = index + 1;
}
// Now also record the final group size
if (currentGroupSize > 0) {
numberOfGroups[currentGroupSize as GroupSize] =
numberOfGroups[currentGroupSize as GroupSize] + 1;
}
// Now apply the jokers.
if (numberOfJokers === 5) {
// Convert them to a group of 5.
numberOfGroups[5] = 1;
} else if (numberOfJokers === 4) {
// Combine them with the 1.
numberOfGroups[1] = 0;
numberOfGroups[5] = 1;
} else if (numberOfJokers === 3) {
// If the other two cards formed a pair, merge them to form a group of 5.
if (numberOfGroups[2] === 1) {
numberOfGroups[2] = 0;
numberOfGroups[5] = 1;
} else {
// Otherwise, take one single and merge it to form a group of 4.
numberOfGroups[1] = 1;
numberOfGroups[4] = 1;
}
} else if (numberOfJokers === 2) {
// If the other cards formed a group of 3, merge them to form a group of 5.
if (numberOfGroups[3] === 1) {
numberOfGroups[3] = 0;
numberOfGroups[5] = 1;
} // Otherwise if there was another pair, merge them to form a group of 4.
else if (numberOfGroups[2] === 1) {
numberOfGroups[2] = 0;
numberOfGroups[4] = 1;
} // Otherwise, take one single and merge it to form a group of 3.
else {
numberOfGroups[1] = numberOfGroups[1] - 1;
numberOfGroups[3] = 1;
}
} else if (numberOfJokers === 1) {
// If the other cards form a group of 4, merge them to form a group of 5.
if (numberOfGroups[4] === 1) {
numberOfGroups[4] = 0;
numberOfGroups[5] = 1;
} // Otherwise if the other cards formed a group of 3, merge them to form a group of 4.
else if (numberOfGroups[3] === 1) {
numberOfGroups[3] = 0;
numberOfGroups[4] = 1;
} // Otherwise if there was at least one pair, take one and use it to form a group of 3.
else if (numberOfGroups[2] >= 1) {
numberOfGroups[2] = numberOfGroups[2] - 1;
numberOfGroups[3] = 1;
} // Otherwise, take one single and merge it to form a group of 2.
else {
numberOfGroups[1] = numberOfGroups[1] - 1;
numberOfGroups[2] = 1;
}
}
// Now from the counted groups, return the corresponding hand type.
if (numberOfGroups[5] === 1) {
return "FIVE_OF_A_KIND";
} else if (numberOfGroups[4] === 1) {
return "FOUR_OF_A_KIND";
} else if (numberOfGroups[3] === 1 && numberOfGroups[2] === 1) {
return "FULL_HOUSE";
} else if (numberOfGroups[3] === 1) {
return "THREE_OF_A_KIND";
} else if (numberOfGroups[2] === 2) {
return "TWO_PAIR";
} else if (numberOfGroups[2] === 1) {
return "ONE_PAIR";
}
return "HIGH_CARD";
}
// Test cases
const orderedTestHands = orderHands(
Object.values(TEST_HANDS).sort(() => (Math.random() > 0.5 ? 1 : -1))
);
let index = 0;
let testSum = 0;
while (index < orderedTestHands.length) {
const hand = orderedTestHands[index];
if (TEST_HANDS[index + 1] !== hand) {
console.error(
"β, expected",
TEST_HANDS[index + 1],
"in rank",
index + 1,
"but got",
hand
);
} else {
console.log("β
");
// For each hand, we want to sum the product of the hand's bid with its rank (index +
// 1). First we need to parse the bid.
const bid = parseInt(hand?.split(/\s+/)[1] || "", 10);
if (Number.isNaN(bid)) {
throw new Error(`could not determine bid for hand: ${hand}`);
}
testSum = testSum + bid * (index + 1);
}
index = index + 1;
}
if (testSum !== 5905) {
console.error("β, got wrong sum: expected", 5905, "but got", testSum);
}
// Now try on our real document with every hand in our set.
import * as fs from "fs";
fs.readFile("./2023/7.txt", (err, rawFile) => {
if (err) throw err;
const unorderedHands = rawFile.toString().split(/\n/);
const hands = orderHands(unorderedHands);
let index = 0;
let sum = 0;
while (index < hands.length) {
const hand = hands[index];
// For each hand, we want to sum the product of the hand's bid with its rank (index +
// 1). First we need to parse the bid.
const bid = parseInt(hand?.split(/\s+/)[1] || "", 10);
if (Number.isNaN(bid)) {
throw new Error(
`could not determine bid for hand: ${hand} at index ${index}`
);
}
sum = sum + bid * (index + 1);
index = index + 1;
}
console.log("sum", sum);
});