summaryrefslogtreecommitdiff
path: root/day-7
diff options
context:
space:
mode:
Diffstat (limited to 'day-7')
-rw-r--r--day-7/script.ts20
1 files changed, 13 insertions, 7 deletions
diff --git a/day-7/script.ts b/day-7/script.ts
index 0d812f7..9b70cfa 100644
--- a/day-7/script.ts
+++ b/day-7/script.ts
@@ -11,6 +11,7 @@ enum HandType {
}
const cardStrength = [
+ "J",
"2",
"3",
"4",
@@ -20,7 +21,6 @@ const cardStrength = [
"8",
"9",
"T",
- "J",
"Q",
"K",
"A",
@@ -28,17 +28,23 @@ const cardStrength = [
function classifyHand(hand: string) {
const cards = hand.split("");
+
+ const jokerCount = cards.filter((card) => card === "J").length;
+
const counts = Array.from(new Set(cards))
+ .filter((card) => card !== "J")
.map((card) => cards.filter((c) => c === card).length)
.sort()
.reverse();
- if (counts[0] === 5) return HandType.fiveOfAKind;
- if (counts[0] === 4) return HandType.fourOfAKind;
- if (counts[0] === 3 && counts[1] === 2) return HandType.fullHouse;
- if (counts[0] === 3) return HandType.threeOfAKind;
- if (counts[0] === 2 && counts[1] === 2) return HandType.twoPair;
- if (counts[0] === 2) return HandType.onePair;
+ const biggestCount = (counts[0] || 0) + jokerCount;
+
+ if (biggestCount == 5) return HandType.fiveOfAKind;
+ if (biggestCount == 4) return HandType.fourOfAKind;
+ if (biggestCount == 3 && counts[1] == 2) return HandType.fullHouse;
+ if (biggestCount == 3) return HandType.threeOfAKind;
+ if (biggestCount == 2 && counts[1] == 2) return HandType.twoPair;
+ if (biggestCount == 2) return HandType.onePair;
return HandType.highCard;
}