-
Notifications
You must be signed in to change notification settings - Fork 1
/
usaco_util.ts
80 lines (77 loc) · 2.47 KB
/
usaco_util.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
import axios from 'axios';
import { writeFileSync } from 'fs';
import { ProblemData } from './ProblemData';
const problems = require('./problems.json') as { [key: string]: ProblemData };
let report = 'added problems:\n```\n';
async function addProblem(id: number) {
try {
const url = `https://usaco.org/index.php?page=viewproblem2&cpid=${id}`;
const response = await (await fetch(url)).text();
const htmlContent: string = response;
const problem = htmlContent.match(/<h2> Problem (\d). (.*) <\/h2>/)!;
const number = problem[1],
title = problem[2];
const contest = htmlContent.match(
/<h2> USACO (\d+) (December|January|February|(?:US Open)) Contest, (Bronze|Silver|Gold|Platinum) <\/h2>/
)!;
const year = contest[1],
month = contest[2],
division = contest[3];
const sample_input =
/<h4>SAMPLE INPUT:<\/h4>\s*<pre class='in'>\n?([\w\W]*?)<\/pre>/g;
const sample_output =
/<h4>SAMPLE OUTPUT:<\/h4>\s*<pre class='out'>\n?([\w\W]*?)<\/pre>/g;
const inputs = [...htmlContent.matchAll(sample_input)!].map(
match => match[1]
);
const outputs = [...htmlContent.matchAll(sample_output)!].map(
match => match[1]
);
// console.log('Problem', number, title, year, month, division);
problems[id] = {
id: id,
url,
source: {
sourceString: `${year} ${month} ${division}`,
year: +year,
contest: month,
division: division,
},
submittable: true,
title: {
titleString: `${number}. ${title}`,
place: +number,
name: title,
},
input: 'stdin',
output: 'stdout',
samples: inputs.map((input, i) => ({
input: input,
output: outputs[i],
})),
};
// console.log('Problem added!');
console.log(
`id ${id}: ${problems[id].title.name} (#${problems[id].title.place} from ${problems[id].source.sourceString})\n`
);
return true;
} catch (error) {
if (!(error instanceof TypeError)) console.log(error);
return false;
}
}
(async () => {
// max id gap between two consecutive contests
const MAX_GAP = 20;
const LAST_ID = Math.max(...Object.keys(problems).map(Number));
let last_added = LAST_ID;
let id = last_added + 1;
while (id - last_added < MAX_GAP) {
if (await addProblem(id++)) {
last_added = id - 1;
}
}
if (last_added == LAST_ID) process.exit(0);
report += '```';
writeFileSync('problems.json', JSON.stringify(problems, null, 2));
})();