-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (46 loc) · 1.49 KB
/
index.js
File metadata and controls
50 lines (46 loc) · 1.49 KB
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
const chalk = require('chalk');
const fs = require('fs');
let startDay = 1;
let endDay = 25;
const args = process.argv.slice(2);
for(let arg of args) {
const n = Number(arg);
if (n > 0) {
startDay = n;
endDay = n;
}
}
const timed = fn => {
const start = process.hrtime();
const output = fn();
const [secs,nanosecs] = process.hrtime(start);
const ms = Math.floor(nanosecs/1000000);
return [output, secs, ms];
}
const showTestResult = (day, part, expected, actual, secs, ms) => {
const durationDesc = chalk.blue(` (${secs}s${ms}ms)`);
if (actual === expected) {
console.log(chalk.green(`day ${day} part ${part}: ${actual}`) + durationDesc);
}
else {
console.log(chalk.red(`day ${day} part ${part}: ${actual} - expected ${expected}` + durationDesc));
}
}
for(let day = startDay; day <= endDay; day++) {
const path = `./${("0" + day).slice(-2)}`;
if (!fs.existsSync(path)) {
console.log(chalk.red(`day ${day} not found`));
continue;
}
const solver = require(path +`/solve`);
const input = fs.readFileSync(path + `/input`)
.toString()
.split('\n')
.map(s => s.replace(/\r$/, ''))
.filter(s => s.length > 0);
for(let part of [1,2]) {
const expected = solver.expected(part);
const [answer,secs,ms] = timed(() => solver.solve(input, part));
showTestResult(day, part, expected, answer, secs, ms);
}
}