4_common.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * A "scratcher" style lottery ticket
  3. */
  4. export type LotteryCard = {
  5. /** The ID of the card */
  6. ID: number,
  7. /** The card's winning numbers */
  8. WinningNumbers: number[],
  9. /** The numbers the player "drew" by playing the card */
  10. PlayedNumbers: number[],
  11. /** The number of winning numbers the player "drew" */
  12. NumberOfWins: number
  13. };
  14. export function ParseLotteryCards(cardStrings: string[]): LotteryCard[] {
  15. const cards: LotteryCard[] = [];
  16. for (const card of cardStrings) {
  17. // Split the card into a "front side", the ID and winning numbers, and a "back side", the drawn numbers
  18. const [frontSide, backSide] = card.split('|');
  19. // Split the front side string into just the numbers inside
  20. const frontSideNumbers = frontSide.split(/\D/).filter((n) => n != '').map((n) => Number(n));
  21. // Split the back side string into just the numbers inside
  22. const playedNumbers = backSide.split(/\D/).filter((n) => n != '').map((n) => Number(n));
  23. const parsedCard = {
  24. // First number is always the ID
  25. ID: frontSideNumbers.shift() || -1,
  26. // The rest of the front side are the winning numbers
  27. WinningNumbers: frontSideNumbers,
  28. // The entire "back side" is the played numbers
  29. PlayedNumbers: playedNumbers,
  30. // Initial value to be filled in later
  31. NumberOfWins: 0,
  32. };
  33. // Count the number of winning numbers the player drew
  34. parsedCard.WinningNumbers.forEach((n) => {
  35. if (parsedCard.PlayedNumbers.includes(n)) { parsedCard.NumberOfWins++ }
  36. });
  37. cards.push(parsedCard);
  38. }
  39. return cards;
  40. }