-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
304 lines (281 loc) · 8.16 KB
/
cli.ts
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import fs from 'fs';
interface Options {
year: number;
day: number;
overwrite: boolean;
}
const CONFIG = await Bun.file('config.json').json();
const args = convertArgs(Bun.argv);
const options = parseOptions(args);
// console.log(`Puzzle year is ${options.year}`);
// console.log(`Puzzle day is ${options.day}`);
const { year, day } = options;
const commands = args['_'];
if (commands.length === 0) {
console.error('No commands given');
printHelp();
process.exit(0);
}
switch (commands[0]) {
case 'input': {
await downloadInput(year, day, options);
break;
}
case 'copy':
copyFile(year, day, commands[1], options);
break;
case 'submit': {
await submit(year, day, commands[1], commands[2]);
break;
}
case 'prompt': {
await downloadPrompt(year, day, options);
await cleanPrompt(year, day);
break;
}
case 'list': {
const ext = commands[1];
await listFiles(year, ext);
break;
}
case 'help':
printHelp();
break;
default:
throw new Error(`Invalid command: ${commands[0]}`);
}
// ----------------- FUNCTIONS -----------------
function printHelp() {
console.log(`
Usage: bun cli.ts [OPTIONS] [COMMAND]
Commands:
input Download puzzle input for the given day
copy <ext> Copies file with given extension from previous day
submit <part> <answer> Submit puzzle answer
prompt Download prompt
list <ext> List solutions for the year. Can filter by extension
help Print this message
Options:
-d, --day <DAY> Puzzle day [default: current day if in December]
-y, --year <YEAR> Puzzle year [default: current year]
-o, --overwrite yes Overwrite file if they already exist
`);
}
async function downloadInput(year: number, day: number, options?: Options) {
let filePath = `${getPuzzlePath(year, day)}/input.txt`;
if (!options?.overwrite && (await Bun.file(filePath).exists())) {
console.log(`Input already exists: ${filePath} (Use to overwrite option (-o) to overwrite)`);
return;
}
let url = `https://adventofcode.com/${year}/day/${day}/input`;
let headers = { cookie: `session=${CONFIG.SESSION_ID}` };
if (CONFIG.USER_AGENT) {
headers['user-agent'] = CONFIG.USER_AGENT;
}
let response = await fetch(url, { headers });
maybeCreateDirectory(year, day);
// let text = (await response.text()) as string;
await Bun.write(filePath, response);
console.log(`File downloaded to: ${filePath}`);
}
function copyFile(year: number, day: number, ext: string, options: Options) {
if (!ext) {
console.error('No ext given');
return;
}
maybeCreateDirectory(year, day);
const puzzlePath = getPuzzlePath(year, day);
const currentPath = `${puzzlePath}/day${day}.${ext}`;
let previousDay = day - 1;
let isComplete = false;
const copyFromYear = (year: number) => {
while (previousDay >= 1) {
const file = `day${previousDay}.${ext}`;
const previousPath = `./${year}/day${previousDay}/${file}`;
if (!options.overwrite && fs.existsSync(currentPath)) {
console.log(
`File already exists: ${currentPath} (Use to overwrite option (-o) to overwrite)`
);
isComplete = true;
break;
}
if (fs.existsSync(previousPath)) {
fs.copyFileSync(previousPath, currentPath);
console.log(`Copied file ${previousPath} to ${currentPath}`);
isComplete = true;
break;
}
previousDay--;
}
};
copyFromYear(year);
// Look in previous year
if (previousDay === 0) {
previousDay = 25;
copyFromYear(year - 1);
}
if (!isComplete) {
console.log('No previous file to copy this year');
}
}
async function submit(year, day, part, answer) {
if (!part || !answer) {
console.error('No part or answer given');
return;
}
let url = `https://adventofcode.com/${year}/day/${day}/answer`;
let body = `level=${part}&answer=${answer}`;
let headers = {
cookie: `session=${CONFIG.SESSION_ID}`,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body),
};
if (CONFIG.USER_AGENT) {
headers['user-agent'] = CONFIG.USER_AGENT;
}
const response = await fetch(url, {
method: 'POST',
body,
headers,
});
const result = await response.text();
try {
let [, matches] = [
...result.replaceAll('\n', '').matchAll(/.*\<article\>(.*)\<\/article\>/g),
][0];
console.log(matches);
return matches;
} catch (error) {
console.error(error);
throw error;
}
}
async function downloadPrompt(year, day, options) {
maybeCreateDirectory(year, day);
const puzzlePath = getPuzzlePath(year, day);
let path = `${puzzlePath}/README.md`;
if (!options.overwrite && fs.existsSync(path)) {
console.log(`Prompt already exists: ${path} (Use to overwrite option (-o) to overwrite)`);
return;
}
let url = `https://adventofcode.com/${year}/day/${day}`;
let headers = { cookie: `session=${CONFIG.SESSION_ID}` };
if (CONFIG.USER_AGENT) {
headers['user-agent'] = CONFIG.USER_AGENT;
}
let response = await fetch(url, { headers });
await Bun.write(path, response);
console.log(`Prompt downloaded to: ${path}`);
}
async function listFiles(year: number, ext?: string) {
if (year) {
let yearPath = `./${year}`;
if (fs.existsSync(yearPath)) {
let days = (await fs.promises.readdir(yearPath)) as string[];
days = days
.map(d => Number(d.replace('day', '')))
.sort((a, b) => a - b)
.map(n => `day${n}`);
for (const day of days) {
let files = (await fs.promises.readdir(`${yearPath}/${day}`)) as string[];
if (ext)
console.log(
day,
files.filter(f => !f.endsWith('.md') && !f.endsWith('.txt') && f.endsWith(`.${ext}`))
);
else
console.log(
day,
files.filter(f => !f.endsWith('.md') && !f.endsWith('.txt'))
);
}
}
}
}
function cleanPrompt(year, day) {
maybeCreateDirectory(year, day);
const puzzlePath = getPuzzlePath(year, day);
let path = `${puzzlePath}/README.md`;
if (!fs.existsSync(path)) {
console.error(`Prompt does not exist: ${path}`);
return;
}
let file = fs.readFileSync(path, 'utf8');
let lines = file.split('\n');
let isMain = false;
for (let i = 0; i < lines.length; ) {
if (lines[i].includes('main>')) isMain = !isMain;
if (!isMain) lines.splice(i, 1);
else i++;
}
lines.push('</main>', '');
fs.writeFileSync(path, lines.join('\n'));
}
function maybeCreateDirectory(year: number, day: number) {
const yearDirectory = `./${year}`;
const puzzlePath = getPuzzlePath(year, day);
if (!fs.existsSync(yearDirectory)) {
fs.mkdirSync(yearDirectory);
}
if (!fs.existsSync(puzzlePath)) {
fs.mkdirSync(puzzlePath);
}
}
function convertArgs(arglist: any[]) {
arglist = arglist.slice(2);
const args: Record<string, any> = { _: [] as any[] };
for (let i = 0; i < arglist.length; i++) {
if (arglist[i].includes('-')) {
args[arglist[i]] = arglist[i + 1];
i++;
} else {
args['_'].push(arglist[i]);
}
}
return args;
}
function parseOptions(args: Record<string, any>): Options {
let today = new Date();
// Provide defaults
let options = {
year: today.getFullYear(),
day: today.getDate(),
overwrite: false,
};
// Puzzle day come out at 5am UTC
if (today.getUTCHours() >= 5) {
options.day += 1;
}
// Puzzles comes out in December (month 11). Try to calculate current year
if (today.getUTCMonth() !== 11) {
options.year -= 1;
}
for (const key of Object.keys(args)) {
if (key === '_') continue;
switch (key) {
case '-d':
case '--day':
options.day = parseInt(args[key], 10);
break;
case '-y':
case '--year':
options.year = parseInt(args[key], 10);
break;
case '-o':
case '--overwrite':
options.overwrite = true;
break;
case '-h':
case '--help':
printHelp();
break;
default:
throw new Error(`Invalid option: ${key}`);
break;
}
}
return options;
}
function getPuzzlePath(year: number, day: number) {
return `./${year}/day${day}`;
}