-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblems.ts
76 lines (63 loc) · 1.92 KB
/
problems.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
import chalk from "chalk";
import fs from "fs/promises";
import { CompareType } from "./check";
import { exec } from "./grader";
export interface Subtask {
grouped?: boolean;
scores: number[];
}
export const ProblemTypes = ["normal", "interactive"] as const;
export interface Problem {
title: string;
description: string;
// * Time Limit in Seconds
timelimit: number;
// * Memory Limit in MB
memorylimit: number;
type?: typeof ProblemTypes[number];
subtasks: { [name: string]: number | Subtask };
// * Default = 100
// TODO Auto Infer maxScore from subtasks
maxScore?: number;
statement?: string;
// * Default = "W"
compare?: CompareType;
}
let problemsList: { [id: string]: Problem } = {};
export async function loadProblems() {
const problems = (await exec("ls problems")).stdout
.split("\n")
.filter((l) => !l.includes(".") && l.length > 0);
if (!problems?.length) {
console.log(
chalk.red("Error, No Problems, I mean.. no problems exist!")
);
}
const problemsLoaded: { [id: string]: Problem } = {};
for (const problemID of problems) {
try {
const buffer = await fs.readFile(
`problems/${problemID}/manifest.json`
);
const problem: Problem = JSON.parse(buffer.toString());
problemsLoaded[problemID] = problem;
} catch (err) {
console.log(chalk.red(`Cannot load ${problemID}: ${err}`));
}
}
problemsList = problemsLoaded;
console.log(
chalk.green(
`Successfully loaded ${Object.keys(problemsList).length} problems`
)
);
}
export function getProblems(id: string) {
return problemsList[id];
}
export function problemExists(id: string): boolean {
return id in problemsList;
}
export function isInteractive(id: string) {
return getProblems(id).type == "interactive";
}