Browse Source

Add day 7 part i solution

My regexes for finding the card ranks where not working, but after some
googling, I got it.
ApisNecros 1 year ago
parent
commit
9a6aad8ba5
1 changed files with 21 additions and 23 deletions
  1. 21 23
      7/7_1.ts

+ 21 - 23
7/7_1.ts

@@ -34,20 +34,16 @@ const tests_reddit = [
 const input = await LoadInput(7);
 
 // Parse the input
-const parsedHands = ParseInput(tests_reddit);
+const parsedHands = ParseInput(input);
 // Sort the cards
 const sortedHands = parsedHands.sort(SortHands);
 
-console.log(sortedHands);
-
 // Find the input
 let totalWinnings = 0;
 sortedHands.forEach((hand, idx) => {
     totalWinnings += hand.Bid * (idx + 1);
 });
 
-// Wrong answers
-// 248305851 -- too high
 console.log(`The total winnings are $${totalWinnings}`);
 
 
@@ -84,33 +80,35 @@ function ParseInput(handList: string[]): CamelCardsHand[] {
 function FindCamelCardRank(hand: string): CamelCardRanking {
     const cardFacesSorted = hand.split("").sort().join("");
 
-    const threeOfKind = new RegExp("(.)(?!\\1\\1)", "g");
-    const twoOfKind = new RegExp("(.)(?!\\1)", "g");
+    const fiveOfKind = new RegExp("(.)\\1{4}", "g");
+    const fourOfKind = new RegExp("(.)\\1{3}", "g");
+    const threeOfKind = new RegExp("(.)\\1{2}", "g");
+    const twoOfKind = new RegExp("(.)\\1{1}", "g");
+    // Can't get these two to work in one regex, so I'll split them for now
+    const fullHouse_A = new RegExp("(.)\\1(.)\\2{2}", "g");
+    const fullHouse_B = new RegExp("(.)\\1{2}(.)\\2", "g");
 
-    if (cardFacesSorted.replace(twoOfKind, "").length == 0) {
+    if (cardFacesSorted.replace(twoOfKind, "").length == 5) {
         return CamelCardRanking.HIGH_CARD;
     }
-    else if(cardFacesSorted.replace(twoOfKind, "").length == 1) {
-        return CamelCardRanking.ONE_PAIR;
+    else if(cardFacesSorted.replace(fiveOfKind, "").length == 0) {
+        return CamelCardRanking.FIVE_OF_A_KIND;
+    }
+    else if(cardFacesSorted.replace(fourOfKind, "").length == 1) {
+        return CamelCardRanking.FOUR_OF_A_KIND;
+    }
+    else if(cardFacesSorted.replace(fullHouse_A, "").length == 0
+        || cardFacesSorted.replace(fullHouse_B, "").length == 0) {
+        return CamelCardRanking.FULL_HOUSE;
     }
-    else if(cardFacesSorted.replace(threeOfKind, "").length == 1) {
+    else if(cardFacesSorted.replace(threeOfKind, "").length == 2) {
         return CamelCardRanking.THREE_OF_A_KIND;
     }
-    // Could technically be a three of a kind, but the threeOfKind case will catch
-    // that, meaning this could only be a two pair
-    else if(cardFacesSorted.replace(twoOfKind, "").length == 2) {
+    else if(cardFacesSorted.replace(twoOfKind, "").length == 1) {
         return CamelCardRanking.TWO_PAIR;
     }
-    // Could technically be a three of a kind, but the threeOfKind case will catch
-    // that, meaning this could only be a full house
-    else if(cardFacesSorted.replace(twoOfKind, "").length == 3) {
-        return CamelCardRanking.FULL_HOUSE;
-    }
     else if(cardFacesSorted.replace(twoOfKind, "").length == 3) {
-        return CamelCardRanking.FOUR_OF_A_KIND;
-    }
-    else if(cardFacesSorted.replace(twoOfKind, "").length == 4) {
-        return CamelCardRanking.FIVE_OF_A_KIND;
+        return CamelCardRanking.ONE_PAIR;
     }
     else {
         throw new Error(`Unsure how we reached this condition!\nSorted hand was: ${cardFacesSorted}`);