forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenges.ts
More file actions
297 lines (269 loc) · 10.1 KB
/
Copy pathchallenges.ts
File metadata and controls
297 lines (269 loc) · 10.1 KB
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
import { and, desc, eq, inArray, lte } from "drizzle-orm";
import { getDb } from "@/server/db";
import { challenges as challengesTable, type ChallengeRow } from "@/server/db/schema";
/**
* Problem records, read from the database (Cloudflare D1 in prod, the libSQL file
* in dev). One row per problem holds the statement (Markdown), samples, AND the
* hidden tests + checker. Publishing a problem is an insert (see
* `scripts/seed-challenges.ts`), not a redeploy.
*
* These readers run on the server. The hidden `tests`/`checker` are only returned
* by the full-record readers (`getChallengeBySlug`, `getDailyChallenge`, …) that
* the judge uses; listings use `getReleasedSummaries` and the client only ever
* receives `toPublicContent` fields — hidden data never reaches the browser.
*/
export interface TestCase {
input: string;
output: string;
}
export interface Sample extends TestCase {
explanation?: string;
}
export interface Checker {
type: "exact" | "token" | "float";
epsilon?: number;
}
/** Fields safe to render to solvers. */
export interface ChallengeContent {
slug: string;
title: string;
difficulty: string;
tags: string[];
date: string; // YYYY-MM-DD
timeLimit?: string;
memoryLimit?: string;
author?: string;
statement: string;
inputFormat?: string;
outputFormat?: string;
constraints?: string;
samples: Sample[];
}
export interface Challenge extends ChallengeContent {
checker: Checker;
/** Hidden tests — server-side only, never serialized to the client. */
tests: TestCase[];
}
/** Lightweight shape for listings — no statement/samples/tests/checker. */
export interface ChallengeSummary {
slug: string;
title: string;
difficulty: string;
tags: string[];
date: string;
author?: string;
}
/** IST is UTC+5:30 and never observes DST, so a fixed offset is exact. */
const IST_OFFSET_MS = 5.5 * 60 * 60 * 1000;
/**
* Today's date as YYYY-MM-DD in IST — the chapter runs on India time, so the
* Problem of the Day rolls over at IST midnight (not UTC midnight). Derived from
* the epoch plus a fixed offset and read with getUTC*, so it's independent of the
* server's own timezone (Cloudflare Workers run in UTC).
*/
export function todayStr(): string {
const ist = new Date(Date.now() + IST_OFFSET_MS);
return `${ist.getUTCFullYear()}-${String(ist.getUTCMonth() + 1).padStart(2, "0")}-${String(ist.getUTCDate()).padStart(2, "0")}`;
}
/** Current year-month "YYYY-MM" in IST — for month-scoped aggregates. */
export function istYearMonth(): string {
return todayStr().slice(0, 7);
}
/** A challenge is live once its date has arrived — future-dated problems (and
* their hidden tests) are never served, so a problem queued for a later date
* doesn't leak through the app before then. */
export function isReleased(c: { date: string }): boolean {
return c.date <= todayStr();
}
// ── Row → domain mapping ──────────────────────────────────────────────────
// JSON columns (tags/samples/tests/checker) are stored as text; parse defensively
// so one malformed row can't throw the whole page.
function safeJson<T>(value: string | null | undefined, fallback: T): T {
if (!value) return fallback;
try {
return JSON.parse(value) as T;
} catch {
return fallback;
}
}
function normalizeTags(raw: unknown): string[] {
return Array.isArray(raw) ? raw.map(String) : [];
}
function normalizeSamples(raw: unknown): Sample[] {
if (!Array.isArray(raw)) return [];
return raw.map((s) => {
const o = (s ?? {}) as Record<string, unknown>;
return {
input: typeof o.input === "string" ? o.input : "",
output: typeof o.output === "string" ? o.output : "",
explanation:
typeof o.explanation === "string" && o.explanation ? o.explanation : undefined,
};
});
}
function normalizeTests(raw: unknown): TestCase[] {
if (!Array.isArray(raw)) return [];
return raw.map((t) => {
const o = (t ?? {}) as Record<string, unknown>;
return {
input: typeof o.input === "string" ? o.input : "",
output: typeof o.output === "string" ? o.output : "",
};
});
}
function normalizeChecker(raw: unknown): Checker {
const o = (raw ?? {}) as { type?: unknown; epsilon?: unknown };
const type = o.type === "exact" || o.type === "float" ? o.type : "token";
return { type, epsilon: typeof o.epsilon === "number" ? o.epsilon : undefined };
}
function rowToChallenge(r: ChallengeRow): Challenge {
return {
slug: r.slug,
title: r.title,
difficulty: r.difficulty,
tags: normalizeTags(safeJson<unknown>(r.tags, [])),
date: r.date,
timeLimit: r.timeLimit ?? undefined,
memoryLimit: r.memoryLimit ?? undefined,
author: r.author ?? undefined,
statement: r.statement,
inputFormat: r.inputFormat ?? undefined,
outputFormat: r.outputFormat ?? undefined,
constraints: r.constraints ?? undefined,
samples: normalizeSamples(safeJson<unknown>(r.samples, [])),
checker: normalizeChecker(safeJson<unknown>(r.checker, {})),
tests: normalizeTests(safeJson<unknown>(r.tests, [])),
};
}
// ── Readers ───────────────────────────────────────────────────────────────
// `date desc, created_at desc` gives a deterministic "most recent" ordering even
// if two problems share a date.
/** Every problem, newest first (includes unreleased — admin/debug use). */
export async function getAllChallenges(): Promise<Challenge[]> {
const db = getDb();
const rows = await db
.select()
.from(challengesTable)
.orderBy(desc(challengesTable.date), desc(challengesTable.createdAt));
return rows.map(rowToChallenge);
}
/** Released problems, newest first (full records). */
export async function getReleasedChallenges(): Promise<Challenge[]> {
const db = getDb();
const rows = await db
.select()
.from(challengesTable)
.where(lte(challengesTable.date, todayStr()))
.orderBy(desc(challengesTable.date), desc(challengesTable.createdAt));
return rows.map(rowToChallenge);
}
/** Released problems as lightweight summaries — for listing pages (no hidden
* data selected at all). */
export async function getReleasedSummaries(): Promise<ChallengeSummary[]> {
const db = getDb();
const rows = await db
.select({
slug: challengesTable.slug,
title: challengesTable.title,
difficulty: challengesTable.difficulty,
tags: challengesTable.tags,
date: challengesTable.date,
author: challengesTable.author,
})
.from(challengesTable)
.where(lte(challengesTable.date, todayStr()))
.orderBy(desc(challengesTable.date), desc(challengesTable.createdAt));
return rows.map((r) => ({
slug: r.slug,
title: r.title,
difficulty: r.difficulty,
tags: normalizeTags(safeJson<unknown>(r.tags, [])),
date: r.date,
author: r.author ?? undefined,
}));
}
/** The Problem of the Day — the most recent released challenge. */
export async function getDailyChallenge(): Promise<Challenge | null> {
const db = getDb();
const rows = await db
.select()
.from(challengesTable)
.where(lte(challengesTable.date, todayStr()))
.orderBy(desc(challengesTable.date), desc(challengesTable.createdAt))
.limit(1);
return rows[0] ? rowToChallenge(rows[0]) : null;
}
/** A single released challenge by slug (unreleased slugs resolve to null, so
* they can't be run or submitted to). */
export async function getChallengeBySlug(slug: string): Promise<Challenge | null> {
const db = getDb();
const rows = await db
.select()
.from(challengesTable)
.where(and(eq(challengesTable.slug, slug), lte(challengesTable.date, todayStr())))
.limit(1);
return rows[0] ? rowToChallenge(rows[0]) : null;
}
/** Titles for a set of slugs in one query — for rendering submission history
* without an N+1 per-row lookup. Slugs with no matching row are simply absent. */
export async function getChallengeTitles(
slugs: string[],
): Promise<Map<string, string>> {
if (slugs.length === 0) return new Map();
const db = getDb();
const rows = await db
.select({ slug: challengesTable.slug, title: challengesTable.title })
.from(challengesTable)
.where(inArray(challengesTable.slug, slugs));
return new Map(rows.map((r) => [r.slug, r.title]));
}
/** Strips hidden fields — only these ever reach the client. */
export function toPublicContent(c: Challenge): ChallengeContent {
return {
slug: c.slug,
title: c.title,
difficulty: c.difficulty,
tags: c.tags,
date: c.date,
timeLimit: c.timeLimit,
memoryLimit: c.memoryLimit,
author: c.author,
statement: c.statement,
inputFormat: c.inputFormat,
outputFormat: c.outputFormat,
constraints: c.constraints,
samples: c.samples,
};
}
/**
* Parses a time limit like "1s", "2 s", or "500ms" into milliseconds. Falls back
* to `fallback` when absent or unparseable.
*/
export function parseTimeLimitMs(timeLimit: string | undefined, fallback = 2000): number {
if (!timeLimit) return fallback;
const match = timeLimit.trim().match(/^([\d.]+)\s*(ms|s)?$/i);
if (!match) return fallback;
const value = parseFloat(match[1]);
if (!Number.isFinite(value)) return fallback;
const unit = (match[2] ?? "s").toLowerCase();
return Math.round(unit === "ms" ? value : value * 1000);
}
/**
* Parses a memory limit like "256 MB", "256MiB", "512m", or a raw byte count
* into bytes. Units are treated as binary (MB = MiB = 1024², the CP convention).
* Falls back to `fallback` when absent or unparseable.
*/
export function parseMemoryLimitBytes(
memoryLimit: string | undefined,
fallback: number,
): number {
if (!memoryLimit) return fallback;
const match = memoryLimit.trim().match(/^([\d.]+)\s*(k|m|g)?i?b?$/i);
if (!match) return fallback;
const value = parseFloat(match[1]);
if (!Number.isFinite(value)) return fallback;
const unit = (match[2] ?? "").toLowerCase();
const mult =
unit === "g" ? 1024 ** 3 : unit === "m" ? 1024 ** 2 : unit === "k" ? 1024 : 1;
return Math.round(value * mult);
}