-
Notifications
You must be signed in to change notification settings - Fork 0
/
day.ts
executable file
·246 lines (216 loc) · 5.94 KB
/
day.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
#!/usr/bin/env -S deno run -A
import $ from '$dax';
import { assertEquals } from '@std/assert';
import { fromFileUrl, parse as pathParse } from '@std/path';
import { parseArgs } from '@std/cli';
import { adjacentFile, type MainArgs, type MainEntry } from './lib/utils.ts';
import { CookieJar, wrapFetch } from '$jar';
import { format } from '@std/fmt/duration';
const YEAR = 2024;
const args = parseArgs(Deno.args, {
boolean: [
'benchmark',
'checkin',
'help',
'new',
'record',
'test',
'trace',
'nowait',
'inputs',
],
string: ['day'],
alias: {
b: 'benchmark',
c: 'checkin',
d: 'day',
h: 'help',
i: 'inputs',
n: 'new',
r: 'record',
t: 'test',
T: 'trace',
},
default: {
trace: false,
day: '',
},
});
if (args.help) {
console.log(`\
day.ts [options] [ARGS]
ARGS passed to day's main function as args._
Options:
-b,--benchmark Run benchmarks
-c,--checkin Do first checkin of the day
-d,--day <number> Day (default: latest day unless --new)
-h,--help Print help text and exit
-i,--inputs Get inputs for the target day. Implied by --new.
-n,--new Wait until drop time, then scaffold today's solution
-r,--record Record results as test data
-t,--test Check test results
-T,--trace Turn on grammar tracing
--nowait Do not wait until drop time, for testing`);
Deno.exit(64);
}
const template: string[] = [];
function wait(ms: number): Promise<void> {
return new Promise((resolve) => {
if (ms <= 0) {
resolve();
} else {
setTimeout(resolve, ms);
}
});
}
async function last(): Promise<string> {
const p = pathParse(fromFileUrl(import.meta.url));
let max = -Infinity;
for await (const f of Deno.readDir(p.dir)) {
let m = f.name.match(/^day(\d+)\.ts$/);
if (m) {
max = Math.max(max, parseInt(m[1], 10));
}
m = f.name.match(/^day0\./);
if (m) {
template.push(f.name);
}
}
return max.toString();
}
if (!args.day) {
args.day = await last();
}
export async function checkin(a: MainArgs): Promise<void> {
await $`deno task check`;
await $`cd inputs && git add day${a.day}.txt && git ci --no-verify -m "Day ${a.day}" && git push`;
await $`git add . && git ci -m "Day ${a.day}" && git push && gh pr create --fill`;
}
export async function newDay(a: MainArgs): Promise<void> {
a.inputs = true;
if (template.length === 0) {
await last();
} else {
a.day = String(parseInt(a.day, 10) + 1);
}
if (!a.nowait) {
const d = new Date(
Date.UTC(YEAR, 11, parseInt(a.day, 10), 5, 0, 0, 300),
);
const ms = d.getTime() - Date.now();
console.log(`Waiting until ${d.toISOString()} (${format(ms)})`);
await wait(ms);
}
await $`open https://adventofcode.com/${YEAR}/day/${a.day}`;
await $`git co -b day${a.day}`;
const copies = template.map((f) => [
new URL(f, import.meta.url),
new URL(f.replace('0', a.day), import.meta.url),
]);
// Copy to new day
await Promise.all(copies.map(([from, to]) => Deno.copyFile(from, to)));
for (const [_from, to] of copies) {
await $`code ${fromFileUrl(to)}`;
}
}
export async function inputs(a: MainArgs): Promise<string> {
const inputFile = adjacentFile(a, 'txt', 'inputs');
try {
await Deno.stat(inputFile);
} catch (_ignored) {
const aoc = Deno.env.get('AOC_COOKIE');
if (!aoc) {
console.error('No AOC_COOKIE environment variable');
Deno.exit(1);
}
const cookieJar = new CookieJar([{
name: 'session',
value: aoc,
domain: 'adventofcode.com',
path: '/',
secure: true,
httpOnly: false,
}]);
const fetch = wrapFetch({ cookieJar });
const inputSrc = `https://adventofcode.com/${YEAR}/day/${a.day}/input`;
console.log(`Fetching ${inputSrc}`);
const headers = new Headers({
'user-agent':
`github.com/hildjj/AdventOfCode${YEAR} by joe-github@cursive.net`,
});
const res = await fetch(inputSrc, { headers });
const input = await res.text();
if (!res.ok) {
console.error(res.status, res.statusText);
console.error(input);
Deno.exit(1);
}
await Deno.writeTextFile(inputFile, input);
}
return inputFile;
}
export async function test(args: MainArgs): Promise<void> {
const mod = (await import(
new URL(`day${args.day}.ts`, import.meta.url).toString()
)).default as MainEntry<unknown>;
try {
if (args.benchmark) {
Deno.bench(
`Day ${args.day}`,
{ permissions: { read: true } },
async () => {
await mod(args);
},
);
}
const results = await mod(args);
const { duration } = performance.measure(`run_${args.day}`, args.day);
if (args.record) {
const str = Deno.inspect(results, {
colors: false,
compact: true,
depth: Infinity,
iterableLimit: Infinity,
strAbbreviateSize: Infinity,
trailingComma: true,
}).replaceAll('[ ', '[').replaceAll(' ]', ']');
await Deno.writeTextFile(
adjacentFile(args, 'js', 'test'),
`export default ${str};\n`,
);
}
if (args.test) {
const expected = await import(adjacentFile(args, 'js', 'test'));
assertEquals(results, expected.default);
}
console.log(Deno.inspect(results, {
colors: Deno.stdout.isTerminal(),
depth: Infinity,
iterableLimit: Infinity,
strAbbreviateSize: Infinity,
trailingComma: true,
}));
// Does not include parser generation or parse time, which are roughly
// a constant 8ms on my box.
console.log(`${duration.toFixed(4)} ms`);
} catch (er) {
console.error(er);
}
}
if (import.meta.main) {
if (args.new) {
await newDay(args);
}
if (args.inputs) {
const inputFile = await inputs(args);
if (args.new) {
await $`code ${inputFile}`;
Deno.exit(0);
}
}
if (args.checkin) {
await checkin(args);
Deno.exit(0);
}
await test(args);
}