diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-12-09 17:28:34 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-12-09 17:28:34 +0300 |
commit | a26b6e174542edd2f560675cde98898672d5e293 (patch) | |
tree | 52046bc1bf7bc6f05fae7791cb98578e0f6eb6f8 | |
parent | c1d1ec78b95cf7656f267549c50b6c20408a9423 (diff) | |
download | aoc-2023-a26b6e174542edd2f560675cde98898672d5e293.tar.gz |
feat(day-7): add part 2 solution
-rw-r--r-- | day-7/script.ts | 20 |
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; } |