-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
210 lines (162 loc) · 5.37 KB
/
main.js
File metadata and controls
210 lines (162 loc) · 5.37 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import fs from 'fs/promises';
import readline from 'readline';
import { stdin, stdout } from 'process';
const STAGE = {
test: 'test',
real: 'real'
};
const CHAR = {
dash: '-'
};
const measureTime = () => {
const startTime = process.hrtime();
return () => {
const [s, ns] = process.hrtime(startTime);
const t = (s * 1e3) + (ns * 1e-6);
return Math.floor(t * 1000) / 1000;
};
};
const timed = (f) => async (...xs) => {
const t = measureTime();
const result = await f(...xs);
const time = t();
return { result, time };
};
const getCode = async ({ year, day, part }) => {
const s = `0${day}`.slice(-2);
const { default: run } = await import(`./${year}/${s}/code/part-${part}.js`);
return timed(run);
};
const getInput = async ({ year, day, options }) => {
const s = `0${day}`.slice(-2);
const b = await fs.readFile(`./${year}/${s}/input/${options.test ? STAGE.test : STAGE.real}`);
return b.toString();
};
const writeOutput = async ({ year, day, part, options }, output) => {
const s = `0${day}`.slice(-2);
await fs.mkdir(`./${year}/${s}/output`, { recursive: true });
await fs.writeFile(`./${year}/${s}/output/part-${part}.json`, Buffer.from(JSON.stringify(output, null, 4)));
};
const createFiles = async ({ year, day }) => {
const s = `0${day}`.slice(-2);
await fs.mkdir(`./${year}/${s}/code`, { recursive: true });
await fs.mkdir(`./${year}/${s}/input`, { recursive: true });
await fs.writeFile(`./${year}/${s}/code/part-1.js`, Buffer.from(`export default (input) => {\n\n}\n`));
await fs.writeFile(`./${year}/${s}/code/part-2.js`, Buffer.from(`export default (input) => {\n\n}\n`));
await fs.writeFile(`./${year}/${s}/input/${STAGE.test}`, '');
await fs.writeFile(`./${year}/${s}/input/${STAGE.real}`, '');
};
const getParams = () => {
const xs = [...process.argv.slice(2)];
const params = [];
const options = {};
const take = () => xs.shift();
const option = (k, v) => {
options[k] = v;
}
const param = (k) => {
params.push(k);
}
const read = () => {
const s = take();
const ds = s.split(CHAR.dash);
const name = ds.pop();
if (!name || ds.length > 2) {
throw new Error(`Cannot parse '${s}'`);
}
if (ds.length === 2) {
return option(name, true);
}
if (ds.length === 1) {
if (!xs.length) {
throw new Error(`No value for input argument '${s}'`);
}
return option(name, take());
}
return param(name);
};
while(xs.length > 0) {
read();
}
return { params, options };
};
const init = async (params, options) => {
const [yearString, dayString] = params;
if (!yearString || !dayString) {
throw new Error(`Provide parameters for year and day.`)
}
const year = parseInt(yearString, 10);
if (isNaN(year) || year < 2015 || year > new Date().getFullYear()) {
throw new Error(`Not a valid year: '${yearString}'`);
}
const day = parseInt(dayString, 10);
if (isNaN(day) || day < 1 || day > 25) {
throw new Error(`Not a valid day number: '${dayString}'`);
}
await new Promise((resolve) => {
const i = readline.createInterface({ input: stdin, output: stdout });
i.question(`About to create code and input files for year ${year}, day ${day}.\nThis will overwrite any existing files. Are you sure? [y/n] `, (r) => {
i.close();
if (!['y', 'yes', 'yes please', 'yea', 'yeah', 'aye', 'ok', 'sure', 'go', 'do it'].includes(r.toLowerCase())) {
console.log(`Ok fine.`);
process.exit();
}
resolve();
});
});
await createFiles({ year, day, options });
console.log(`Done.`);
};
const run = async (params, options) => {
const [yearString, dayString, partString] = params;
if (!yearString || !dayString || !partString) {
throw new Error(`Provide parameters for year, day and part number.`)
}
const year = parseInt(yearString, 10);
if (isNaN(year) || year < 2015 || year > new Date().getFullYear()) {
throw new Error(`Not a valid year: '${yearString}'`);
}
const day = parseInt(dayString, 10);
if (isNaN(day) || day < 1 || day > 25) {
throw new Error(`Not a valid day number: '${dayString}'`);
}
const part = parseInt(partString, 10);
if (isNaN(part) || part < 1 || part > 2) {
throw new Error(`Not a valid part number: '${partString}'`);
}
const input = await getInput({ year, day, part, options });
const code = await getCode({ year, day, part, options });
console.log(`Running year ${year}, day ${day}, part ${part} with ${options.test ? 'test' : 'real'} input (size ${input.length})`);
const { time, result } = await code(input);
console.log(`Elapsed: ${time}ms, result: ${result}`);
if (!options.test) {
await writeOutput({ year, day, part, options }, { time, result });
}
};
const runDay = async (params, options) => {
await run([...params, 1], options);
await run([...params, 2], options);
}
const runYear = async (params, options) => {
for (let day = 1; day < 26; day++) {
await runDay([...params, day], options);
}
}
(async () => {
const { params, options } = getParams();
const [cmd, ...ps] = params;
switch (cmd) {
case 'init':
return init(ps, options);
case 'part':
return run(ps, options);
case 'day':
return runDay(ps, options);
case 'year':
return runYear(ps, options);
default:
throw new Error(`Unsupported command '${cmd}'`);
}
})().catch((e) => {
throw e;
});