-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
63 lines (52 loc) · 1.5 KB
/
run.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
const moment = require("moment");
const { readFileSync, readdirSync, lstatSync } = require("fs");
const { join } = require("path");
function run(year, day, part) {
let log = process.env.NODE_ENV !== "test";
part = part.toString().slice(-1);
day = day.toString().length === 1 ? "0".concat(day) : day;
year = year.toString().length === 2 ? "20".concat(year) : year;
let input = readFileSync(`./${year}/${day}/input.txt`, "utf-8")
.trimRight()
.split("\n");
let startTime = moment();
if (log) console.log(`${year}.${day}.${part} running...`);
let answer = require(`./${year}/${day}/part${part}`)(
input.length === 1 ? input[0] : input
);
if (log) console.log(`${year}.${day}.${part} answer: `, answer);
let endTime = moment();
if (log)
console.log(
`${year}.${day}.${part} time: `,
moment(endTime.diff(startTime)).format("mm:ss:SSSS")
);
return answer;
}
const getDirectories = (s) =>
readdirSync(s)
.map((n) => join(s, n))
.filter((p) => lstatSync(p).isDirectory());
module.exports = (year, day, part) => {
if (year === "latest") {
year = Math.max.apply(
null,
getDirectories("./")
.map((d) => parseInt(d, 10))
.filter(isFinite)
);
day = Math.max.apply(
null,
getDirectories(`./${year}/`)
.map((d) => parseInt(d.slice(-2), 10))
.filter(isFinite)
);
}
if (!part) {
run(year, day, 1);
console.log();
run(year, day, 2);
} else {
return run(year, day, part);
}
};