12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- /**
- * A "scratcher" style lottery ticket
- */
- export type LotteryCard = {
- /** The ID of the card */
- ID: number,
- /** The card's winning numbers */
- WinningNumbers: number[],
- /** The numbers the player "drew" by playing the card */
- PlayedNumbers: number[],
- /** The number of winning numbers the player "drew" */
- NumberOfWins: number
- };
- export function ParseLotteryCards(cardStrings: string[]): LotteryCard[] {
- const cards: LotteryCard[] = [];
- for (const card of cardStrings) {
- // Split the card into a "front side", the ID and winning numbers, and a "back side", the drawn numbers
- const [frontSide, backSide] = card.split('|');
- // Split the front side string into just the numbers inside
- const frontSideNumbers = frontSide.split(/\D/).filter((n) => n != '').map((n) => Number(n));
- // Split the back side string into just the numbers inside
- const playedNumbers = backSide.split(/\D/).filter((n) => n != '').map((n) => Number(n));
- const parsedCard = {
- // First number is always the ID
- ID: frontSideNumbers.shift() || -1,
- // The rest of the front side are the winning numbers
- WinningNumbers: frontSideNumbers,
- // The entire "back side" is the played numbers
- PlayedNumbers: playedNumbers,
- // Initial value to be filled in later
- NumberOfWins: 0,
- };
- // Count the number of winning numbers the player drew
- parsedCard.WinningNumbers.forEach((n) => {
- if (parsedCard.PlayedNumbers.includes(n)) { parsedCard.NumberOfWins++ }
- });
- cards.push(parsedCard);
- }
- return cards;
- }
|