From c723339b51e395e4e74efedae529597753d81c84 Mon Sep 17 00:00:00 2001 From: Anson Gao Date: Wed, 6 Sep 2023 00:22:12 +0800 Subject: [PATCH] calculate total bonus and cost if buy all numbers --- .gitignore | 1 + index.js | 60 +++++++++++++++++++++++++++++++---------------- package-lock.json | 29 +++++++++++++++++++++++ package.json | 3 +-- 4 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 .gitignore create mode 100644 package-lock.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.js b/index.js index ad0304d..e31f189 100644 --- a/index.js +++ b/index.js @@ -1,28 +1,48 @@ -var numeral = require('numeral'); -var Combinatorics = require('js-combinatorics'); +const { Combination } = require('js-combinatorics'); + +function printProgress(progress) { + process.stdout.clearLine(0); + process.stdout.cursorTo(0); + process.stdout.write(progress); +} (function () { 'use strict'; - var winnings = []; // NTD - winnings.push({m: 6, s: null, bouns: 39299711}); - winnings.push({m: 5, s: 1, bouns: 3230113}); - winnings.push({m: 5, s: null, bouns: 49011}); - winnings.push({m: 4, s: 1, bouns: 11777}); - winnings.push({m: 4, s: null, bouns: 1655}); - winnings.push({m: 3, s: 1, bouns: 1000}); - winnings.push({m: 2, s: 1, bouns: 400}); - winnings.push({m: 3, s: null, bouns: 400}); + const costPerTicket = 50; // NTD + const randomNumbers = [1, 2, 3, 4, 5, 6]; + const randomSpecialNumber = 7; + const prizes = {}; // NTD + prizes['6:0'] = { name: '頭獎', m: 6, s: null, bonus: 104538797, acc: 0 }; + prizes['5:1'] = { name: '貳獎', m: 5, s: 1, bonus: 10089281, acc: 0 }; + prizes['5:0'] = { name: '參獎', m: 5, s: null, bonus: 120323, acc: 0 }; + prizes['4:1'] = { name: '肆獎', m: 4, s: 1, bonus: 18129, acc: 0 }; + prizes['4:0'] = { name: '伍獎', m: 4, s: null, bonus: 2000, acc: 0 }; + prizes['3:1'] = { name: '陸獎', m: 3, s: 1, bonus: 1000, acc: 0 }; + prizes['2:1'] = { name: '柒獎', m: 2, s: 1, bonus: 400, acc: 0 }; + prizes['3:0'] = { name: '普獎', m: 3, s: null, bonus: 400, acc: 0 }; + + const allNumbers = new Array(49).fill(1).map(function (v, i) {return v + i;}); + const allCombinations = new Combination(allNumbers, 6); - var allNumbers = new Array(49).fill(1).map(function (v, i) {return v + i;}), - allCombinations = Combinatorics.bigCombination(allNumbers, 6); + console.log(`Cost for buying all combinations: NTD ${ + new Intl.NumberFormat().format(allCombinations.length * BigInt(costPerTicket)) + }`); - function hasBalls(nums, matchs) { - return matchs.every(function (match) { - return nums.indexOf(match) > -1; - }); + let totalBonus = 0n; + for(let i = 0n; i < allCombinations.length; i++) { + const comb = allCombinations.nth(i); + const matchedNumbers = comb.filter(n => randomNumbers.includes(n)).length; + const matchedSpecialNumber = comb.includes(randomSpecialNumber) ? 1 : 0; + const prizeKey = `${matchedNumbers}:${matchedSpecialNumber}`; + const matchedPrize = prizes[prizeKey]; + if (matchedPrize) { + matchedPrize.acc += matchedPrize.bonus; + totalBonus += BigInt(matchedPrize.bonus); + printProgress(`Progress: ${i + 1n}/${allCombinations.length}`); + } } - console.log(allCombinations.filter(function (nums) { - return hasBalls(nums, [1, 2, 3, 4, 5, 6]); - })); + console.log(`Bonus if buying all tickets: NTD ${ + new Intl.NumberFormat().format(totalBonus) + }`); }()); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..2703cd8 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,29 @@ +{ + "name": "lottery", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "lottery", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "js-combinatorics": "2.1.1" + }, + "devDependencies": {} + }, + "node_modules/js-combinatorics": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/js-combinatorics/-/js-combinatorics-2.1.1.tgz", + "integrity": "sha512-jKLV+oIyz0TCr7GBhyuOwngntu1rCxSO+lbVTS232HYowA13CTk0RSmPq6nuXGLFaq2ZWLDQqEwJ6LCDnYR0+A==" + } + }, + "dependencies": { + "js-combinatorics": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/js-combinatorics/-/js-combinatorics-2.1.1.tgz", + "integrity": "sha512-jKLV+oIyz0TCr7GBhyuOwngntu1rCxSO+lbVTS232HYowA13CTk0RSmPq6nuXGLFaq2ZWLDQqEwJ6LCDnYR0+A==" + } + } +} diff --git a/package.json b/package.json index 7120dcf..efcaf55 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,7 @@ "description": "try to analyze the winnings of lottery", "main": "index.js", "dependencies": { - "js-combinatorics": "^0.5.2", - "numeral": "^2.0.6" + "js-combinatorics": "2.1.1" }, "devDependencies": {}, "scripts": {