feat(euler): allow for many challenges to be run at once

-
This commit is contained in:
newt 2024-10-09 18:02:41 +01:00
parent 5795d11620
commit bd6470879a
7 changed files with 75 additions and 22 deletions

View file

@ -14,6 +14,7 @@
"chalk": "^4.1.2",
"cheerio": "^1.0.0-rc.10",
"execution-time": "^1.4.1",
"ms": "^2.1.3",
"regenerator-runtime": "^0.13.9",
"ts-node": "^10.4.0",
"typescript": "^4.4.4"

View file

@ -8,6 +8,7 @@ specifiers:
chalk: ^4.1.2
cheerio: ^1.0.0-rc.10
execution-time: ^1.4.1
ms: ^2.1.3
regenerator-runtime: ^0.13.9
ts-node: ^10.4.0
typescript: ^4.4.4
@ -20,6 +21,7 @@ devDependencies:
chalk: 4.1.2
cheerio: 1.0.0-rc.10
execution-time: 1.4.1
ms: 2.1.3
regenerator-runtime: 0.13.9
ts-node: 10.4.0_626351e049b80b142acb2ce48a7f5656
typescript: 4.4.4
@ -518,6 +520,13 @@ packages:
}
dev: true
/ms/2.1.3:
resolution:
{
integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
}
dev: true
/nth-check/2.0.1:
resolution:
{

View file

@ -32,7 +32,7 @@ The source code can be found in the [src](src) directory. My thoughts about some
- [x] [18 - Maximum path sum I](src/18%20-%20Maximum%20path%20sum%20I.ts)
- [x] [19 - Counting Sundays](src/19%20-%20Counting%20Sundays.ts)
- [x] [20 - Factorial digit sum](src/20%20-%20Factorial%20digit%20sum.ts)
- [ ] 21 - Amicable numbers
- [x] [21 - Amicable numbers](src/21%20-%20Amicable%20numbers.ts)
- [ ] 22 - Names scores
- [ ] 23 - Non-abundant sums
- [ ] 24 - Lexicographic permutations

View file

@ -1,7 +1,8 @@
const fs = require('fs');
const chalk = require('chalk');
const { spawnSync } = require('child_process');
const perf = require('execution-time')();
const executionTime = require('execution-time')();
const ms = require('ms');
const generateBanner = text => {
// Calculate the length of the divider
@ -20,42 +21,84 @@ const run = file => {
console.log(generateBanner(file.split('.ts')[0]));
// Execute the file
perf.start();
executionTime.start();
spawnSync('npx', ['ts-node', `"src/${file}"`], {
shell: true,
stdio: 'inherit'
});
const results = perf.stop();
const results = executionTime.stop();
// Print time results
console.log();
console.log(chalk.bold(chalk.yellow(`Executed in ${results.words}`)));
return results.time;
};
const runMany = files => {
let totalTime = 0;
files.forEach(file => {
const time = run(file);
totalTime += time;
console.log();
});
console.log(
chalk.magenta(
chalk.bold(
`This set of executions took roughly ${ms(totalTime, { long: true })} in total!`
)
)
);
};
// Get files
const tsFiles = fs
const allFiles = fs
.readdirSync('src')
.filter(f => f.endsWith('.ts'))
.filter(f => f !== 'utils.ts');
// Extract the puzzle number
const puzzleNumber = process.argv[2];
process.argv.shift();
process.argv.shift();
if (puzzleNumber === 'all' || !puzzleNumber) {
tsFiles
.sort((a, b) => {
a = parseInt(a.split('-')[0]);
b = parseInt(b.split('-')[0]);
if (process.argv[0] === 'all' || process.argv.length === 0) {
const files = allFiles.sort((a, b) => {
a = parseInt(a.split('-')[0]);
b = parseInt(b.split('-')[0]);
return a > b ? 1 : -1;
})
.forEach(file => {
run(file);
console.log();
});
} else if (!isNaN(puzzleNumber)) {
return a > b ? 1 : -1;
});
runMany(files);
} else if (process.argv.length > 1 || process.argv[0].includes(',')) {
let puzzleNumbers = process.argv
.map(v =>
v.includes(',')
? v.split(',').map(e => (!isNaN(e) ? parseInt(e) : null))
: !isNaN(v)
? parseInt(v)
: null
)
.flat()
.filter(e => e !== null);
puzzleNumbers = puzzleNumbers.filter((e, i) => puzzleNumbers.indexOf(e) === i);
const files = allFiles.filter(f =>
f.match(
`^(?:[${puzzleNumbers.map((number, i) =>
i === puzzleNumbers.length - 1 ? number : `${number}|`
)}]) -`
)
);
runMany(files);
} else if (!isNaN(process.argv[0])) {
// Find the associated puzzle
const [file] = tsFiles.filter(f => f.startsWith(puzzleNumber));
const [file] = allFiles.filter(f => f.startsWith(process.argv[0]));
run(file);
} else {
console.log(

View file

@ -4,7 +4,7 @@ export {};
/**
* Use the Sieve of Eratosthenes to find the sum of primes up until a limit.
* @see https://github.com/newtykins/the-honk/tree/main/projects/euler/thoughts/10%20-Summation%20of%29primes.md
* @see https://github.com/newtykins/the-honk/tree/main/challenges/euler/thoughts/10%20-%20Summation%20of%29primes.md
*/
const sumOfPrimes = (limit: number) => {
let array: boolean[] = [];

View file

@ -13,7 +13,7 @@ const factorial = (n: number) => {
/**
* Count the lattice paths using the formula shown in the thoughts document.
* @see https://github.com/newtykins/the-honk/tree/main/projects/euler/thoughts/15%20-Lattice%20paths.md
* @see https://github.com/newtykins/the-honk/tree/main/challenges/euler/thoughts/15%20-%20Lattice%20paths.md
*/
const countLatticePaths = (width: number, height: number) => {
return factorial(width + height) / (factorial(width) * factorial(height));

View file

@ -6,7 +6,7 @@ export {};
/**
* Find a Pythagorean triplet based on its sum
* @see // See https://github.com/newtykins/the-honk/tree/main/projects/euler/thoughts/9%20-Special%20Pythagorean%29triplet.md
* @see https://github.com/newtykins/the-honk/tree/main/challenges/euler/thoughts/9%20-%20Special%20Pythagorean%29triplet.md
*/
const pythagoreanTriplet = (sum: number) => {
let a: number,