4_2.ts 2.1 KB

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