the-honk/challenges/euler/run.js

67 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-10-09 17:02:34 +00:00
const fs = require('fs');
const chalk = require('chalk');
2024-10-09 17:02:37 +00:00
const { spawnSync } = require('child_process');
2024-10-09 17:02:35 +00:00
const perf = require('execution-time')();
2024-10-09 17:02:34 +00:00
2024-10-09 17:02:37 +00:00
const generateBanner = text => {
// Calculate the length of the divider
2024-10-09 17:02:34 +00:00
let divider = '--';
2024-10-09 17:02:37 +00:00
for (let j = 0; j < text.length; j++) {
2024-10-09 17:02:34 +00:00
divider += '-';
}
2024-10-09 17:02:37 +00:00
return `${divider}
${chalk.bold(chalk.greenBright(text))}
${divider}`;
};
const run = file => {
console.log(generateBanner(file.split('.ts')[0]));
2024-10-09 17:02:34 +00:00
2024-10-09 17:02:35 +00:00
// Execute the file
perf.start();
2024-10-09 17:02:37 +00:00
spawnSync('npx', ['ts-node', `"src/${file}"`], {
shell: true,
stdio: 'inherit'
});
2024-10-09 17:02:35 +00:00
const results = perf.stop();
// Print time results
console.log();
console.log(chalk.bold(chalk.yellow(`Executed in ${results.words}`)));
2024-10-09 17:02:34 +00:00
};
2024-10-09 17:02:37 +00:00
// Get files
const tsFiles = fs
.readdirSync('src')
.filter(f => f.endsWith('.ts'))
.filter(f => f !== 'utils.ts');
2024-10-09 17:02:34 +00:00
2024-10-09 17:02:37 +00:00
// Extract the puzzle number
const puzzleNumber = process.argv[2];
2024-10-09 17:02:37 +00:00
2024-10-09 17:02:37 +00:00
if (puzzleNumber === 'all' || !puzzleNumber) {
tsFiles
.sort((a, b) => {
a = parseInt(a.split('-')[0]);
b = parseInt(b.split('-')[0]);
2024-10-09 17:02:37 +00:00
2024-10-09 17:02:37 +00:00
return a > b ? 1 : -1;
})
.forEach(file => {
run(file);
2024-10-09 17:02:37 +00:00
console.log();
2024-10-09 17:02:37 +00:00
});
} else if (!isNaN(puzzleNumber)) {
// Find the associated puzzle
const [file] = tsFiles.filter(f => f.startsWith(puzzleNumber));
run(file);
} else {
console.log(
chalk.bold(
chalk.red('Please ensure that you input the number of the puzzle to run - thank you.')
)
);
}