Browse Source

Add day 4 part ii solution

ApisNecros 1 year ago
parent
commit
276ef73c27
1 changed files with 57 additions and 0 deletions
  1. 57 0
      4/4_2.ts

+ 57 - 0
4/4_2.ts

@@ -0,0 +1,57 @@
+import { DeepClone, Inspect, LoadInput } from "../common.ts";
+import { LotteryCard, ParseLotteryCards } from "./4_common.ts";
+
+const tests = [
+    "Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53",
+    "Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19",
+    "Card 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1",
+    "Card 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83",
+    "Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36",
+    "Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11",
+];
+
+const input = await LoadInput(4);
+
+// Parse the cards
+let parsedCards = ParseLotteryCards(tests);
+// Process the winnings for each card
+parsedCards = ProcessWinnings(parsedCards);
+
+console.log(`Based on your winning cards, you now have ${parsedCards.length} total cards!`);
+
+/**
+ * Processing the winnings from a card
+ *
+ * When a card wins with N amount of numbers, the player then gets a
+ * copy of the next N cards (based on ID). This function goes through
+ * each card, and creates the copies based on the players cards.
+ *
+ * @param {LotteryCard[]} cards The stack of cards the player has
+ * @returns {LotteryCard[]} A new stack of cards with each card copied accordingly.
+ */
+function ProcessWinnings(cards: LotteryCard[]): LotteryCard[] {
+    const originalStack = DeepClone(cards);
+    for (let i = 0; i < cards.length; i++) {
+        // Push a clone of the next N cards based on the number of wins this card has
+        for (let cloneIDOffset = 1; cloneIDOffset <= cards[i].NumberOfWins; cloneIDOffset++) {
+            const cardToClone = FindCardByID(cards[i].ID + cloneIDOffset, originalStack);
+
+            if(cardToClone) {
+                cards.push(cardToClone);
+            }
+        }
+    }
+    
+    return cards;
+}
+
+/**
+ * Find a lottery card in a stack based on its ID
+ *
+ * @param {number} needle The ID of the card to find
+ * @param {LotteryCard[]} haystack The complete stack of cards
+ * @returns {LotteryCard|undefined} The matching card, or undefined if none was found
+ */
+function FindCardByID(needle: number, haystack: LotteryCard[]): LotteryCard|undefined {
+    return haystack.find((card) => card.ID == needle);
+}