This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.js
79 lines (64 loc) · 1.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import fs from "fs";
import path from "path";
import requireDirectory from "require-directory";
import runner from "./src/runner.js";
import validator from "./src/validator.js";
const LOG_DIR = "logs";
/**
* Given a file name, deletes any existing file and creates a new blank one.
*
* @param {string} logFile - The name of the file to delete and re-create.
*
* @return {undefined}
*/
const resetLogFile = logFile => {
if (!fs.existsSync(LOG_DIR)) {
fs.mkdirSync(LOG_DIR);
}
fs.writeFileSync(logFile, "");
};
/**
* Given a mine, a name, and a logFile, runs the miner through the mine.
*
* @param {array} mine - A n x m multidimensional array respresenting the mine.
* @param {string} name - The name of the mine.
*
* @return {number} The score achieved in the mine.
*/
const runMine = async (mine, name) => {
const logFile = path.join(__dirname, LOG_DIR, `${name}.txt`);
resetLogFile(logFile);
const mineScore = await runner.run(mine, logFile);
const valid = await validator.validate(mine, logFile, mineScore);
if (valid) {
console.log(`Mine '${name}' score:`, mineScore);
return mineScore;
}
console.log("No cheating!");
return 0;
};
const main = async () => {
console.log("Riipen Gold Miner");
// Keep track of the total score.
let totalScore = 0;
if (process.argv.slice(2).length > 0) {
// Run a single mine
const name = process.argv.slice(2)[0];
const mine = require(`./mines/${name}.js`).default;
totalScore += await runMine(mine, name);
} else {
// Run all mines
const mines = Object.entries(requireDirectory(module, "./mines"));
for (const [k, mine] of mines) {
totalScore += await runMine(mine.default, k);
}
}
console.log("Final score:", totalScore);
};
(async () => {
try {
await main();
} catch (e) {
console.error(e);
}
})();