-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
281 lines (234 loc) · 6.32 KB
/
index.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
import run from "aocrunner";
const parseInput = (rawInput: string) => rawInput;
const types = [
"five of a kind",
"four of a kind",
"full house",
"three of a kind",
"two pair",
"one pair",
"high card",
] as const;
const cards = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3","2", "J"] as const;
type HandType = (typeof types)[number];
type Card = (typeof cards)[number];
type CardWithoutJ = Exclude<Card, "J">;
type Cards = [Card, Card, Card, Card, Card];
type CardsWithoutJ = [CardWithoutJ, CardWithoutJ, CardWithoutJ, CardWithoutJ, CardWithoutJ];
type Hand = {
cards: Cards;
cardsWithoutJ?: CardsWithoutJ;
type?: HandType;
typeIndex?: number;
bid: number;
};
const detectType = (h: Hand, part2 = false) => {
let cards = [...h.cards];
// replace any "J" cards with the most common card other than "J"
if (part2) {
const mostCommonCard = [...cards.filter((card) => card !== "J")]
.sort(
(a, b) => cards.filter((v) => v === a).length -
cards.filter((v) => v === b).length,
)
.pop();
console.log(mostCommonCard)
console.log(cards)
let cardsWithoutJ = cards.map((card) => (card === "J" ? mostCommonCard : card)) as Cards;
console.log(cardsWithoutJ)
h.cardsWithoutJ = cardsWithoutJ as CardsWithoutJ;
cards = cardsWithoutJ;
}
// if ever card is the same
if (cards.every((card, i, cards) => card === cards[0]))
return "five of a kind";
// if any four of the five cards are the same
if (
cards.some(
(card, i, cards) => cards.filter((c) => c === card).length === 4,
)
)
return "four of a kind";
// if three cards have the same label, and the remaining two cards share a different label
if (
cards.some(
(card, i, cards) => cards.filter((c) => c === card).length === 3,
) &&
cards.some(
(card, i, cards) => cards.filter((c) => c === card).length === 2,
)
)
return "full house";
// if any three of the five cards are the same
if (
cards.some(
(card, i, cards) => cards.filter((c) => c === card).length === 3,
)
)
return "three of a kind";
// if two pairs of the five cards are the same
if (
cards.some(
(card, i, cards) => cards.filter((c) => c === card).length === 2,
)
) {
// remove the first pair
const pairCard = cards.find(
(card, i, cards) => cards.filter((c) => c === card).length === 2,
);
const remainingCards = cards.filter((card) => card !== pairCard);
// if the remaining cards have a pair
if (
remainingCards.some(
(card, i, cards) => cards.filter((c) => c === card).length === 2,
)
)
return "two pair";
}
// if two cards have the same label
if (
cards.some(
(card, i, cards) => cards.filter((c) => c === card).length === 2,
)
)
return "one pair";
// if all cards are different
return "high card";
};
const sortHands = (hands: Required<Hand>[]) => {
// sort by type index
const sortedHands = hands.sort((a, b) => a.typeIndex - b.typeIndex);
// if two hands have the same type, sort by card 1
sortedHands.sort((a, b) => {
if (a.typeIndex === b.typeIndex) {
return cards.indexOf(a.cards[0]) - cards.indexOf(b.cards[0]);
}
return 0;
});
// if two hands have the same type and card 1, sort by card 2
sortedHands.sort((a, b) => {
if (a.typeIndex === b.typeIndex && a.cards[0] === b.cards[0]) {
return cards.indexOf(a.cards[1]) - cards.indexOf(b.cards[1]);
}
return 0;
});
// if two hands have the same type, card 1, and card 2, sort by card 3
sortedHands.sort((a, b) => {
if (
a.typeIndex === b.typeIndex &&
a.cards[0] === b.cards[0] &&
a.cards[1] === b.cards[1]
) {
return cards.indexOf(a.cards[2]) - cards.indexOf(b.cards[2]);
}
return 0;
});
// if two hands have the same type, card 1, card 2, and card 3, sort by card 4
sortedHands.sort((a, b) => {
if (
a.typeIndex === b.typeIndex &&
a.cards[0] === b.cards[0] &&
a.cards[1] === b.cards[1] &&
a.cards[2] === b.cards[2]
) {
return cards.indexOf(a.cards[3]) - cards.indexOf(b.cards[3]);
}
return 0;
});
// if two hands have the same type, card 1, card 2, card 3, and card 4, sort by card 5
sortedHands.sort((a, b) => {
if (
a.typeIndex === b.typeIndex &&
a.cards[0] === b.cards[0] &&
a.cards[1] === b.cards[1] &&
a.cards[2] === b.cards[2] &&
a.cards[3] === b.cards[3]
) {
return cards.indexOf(a.cards[4]) - cards.indexOf(b.cards[4]);
}
return 0;
});
return sortedHands;
};
const part1 = (rawInput: string) => {
const input = parseInput(rawInput).split("\n");
const hands: Hand[] = input.map((line) => {
const [cards, bid] = line.split(" ");
return {
cards: cards.split("") as Cards,
bid: parseInt(bid),
};
});
const handsWithTypes: Required<Hand>[] = hands.map((hand) => {
const type = detectType(hand);
return ({
...hand,
type,
typeIndex: types.indexOf(type),
});
});
const sortedHands = sortHands(handsWithTypes).reverse();
console.log(sortedHands)
let result = 0;
sortedHands.forEach((hand, i) => {
let rank = i+1;
result = result + (hand.bid * rank);
});
return result;
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput).split("\n");
const hands: Hand[] = input.map((line) => {
const [cards, bid] = line.split(" ");
return {
cards: cards.split("") as Cards,
bid: parseInt(bid),
};
});
const handsWithTypes: Required<Hand>[] = hands.map((hand) => {
const type = detectType(hand, true);
return ({
...hand,
type,
typeIndex: types.indexOf(type),
});
});
const sortedHands = sortHands(handsWithTypes).reverse();
console.log(sortedHands)
let result = 0;
sortedHands.forEach((hand, i) => {
let rank = i+1;
result = result + (hand.bid * rank);
});
return result;
};
run({
part1: {
tests: [
{
input: `32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483`,
expected: 6440,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483`,
expected: 5905,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});