-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadTodayInput.js
66 lines (51 loc) · 1.38 KB
/
loadTodayInput.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
import fs from 'fs';
import path from 'path';
let day = (new Date()).getDate();
let year = (new Date()).getFullYear();
let DD = String(day).padStart(2, '0');
let url = `https://adventofcode.com/${year}/day/${day}/input`;
let token = process.env.SESSION_TOKEN;
if (!token) {
console.error('SESSION_TOKEN env variable is not set');
process.exit(1);
}
console.log('Loading...', url);
fetch(url, {
headers: {
cookie: `session=${token}`
}
})
.then((res) => res.text())
.then((text) => {
console.log('Loaded');
text = text.trim();
let inputFolder = new URL(`./inputs/${year}`, import.meta.url).pathname;
fs.mkdirSync(inputFolder, { recursive: true });
fs.writeFileSync(path.join(inputFolder, `day${DD}.txt`), text);
console.log('Saved!');
createTodayJs(day);
console.log(`day${DD}.js has been created, enjoy!`);
})
.catch((err) => {
console.error(err);
});
function createTodayJs(day) {
let content = `import { runDay } from '../../utils.js';
// https://adventofcode.com/2023/day/${day}
console.log(part1(\`test\`))
runDay(2023, ${day})
.part(1, part1)
.part(2, part2)
.end();
function part1(inp) {
return 123;
}
function part2(inp) {
return 123;
}
`
;
let folder = new URL(`./${year}/js/`, import.meta.url).pathname;
fs.mkdirSync(folder, { recursive: true });
fs.writeFileSync(path.join(folder, `day${DD}.js`), content);
}