Repository navigation
Expand file tree
/
Copy pathtemplate.typ
More file actions
810 lines (738 loc) · 20.5 KB
/
Copy pathtemplate.typ
File metadata and controls
810 lines (738 loc) · 20.5 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#import "@preview/unify:0.7.0": *
#import "@preview/text-dirr:1.0.0": text-dir
#let override_localization = state("locale_override", (:))
#let get_locale(lang: none, region: none) = {
let lang = if lang != none { lang } else { text.lang }
let region = if region != none { region } else { text.region }
if lang == "zh" {
if region in ("TW", "HK", "MO") {
"zh-" + lower(region)
} else {
"zh-cn"
}
} else {
lang
}
}
#let get_font(locale) = {
let fonts = (
"ar": "Sakkal Majalla",
"fa": "Sakkal Majalla",
"zh-cn": "Noto Serif CJK SC",
"zh-tw": "Noto Serif CJK TC",
"ja": "Noto Serif CJK JP",
"ko": "Noto Serif CJK KR",
"hi": "Noto Serif Devanagari",
"bn": "Noto Serif Bengali",
"th": "Noto Serif Thai",
"he": "Noto Serif Hebrew",
"hy": "Noto Serif Armenian",
"ka": "Noto Serif Georgian",
"si": "Noto Serif Sinhala",
"ta": "Noto Serif Tamil",
)
if locale in fonts {
(fonts.at(locale), "Latin Modern Roman")
} else {
("Latin Modern Roman",)
}
}
#let apply_font_rules(doc) = {
show text: it => {
let lang = it.at("lang", default: none)
if lang == none { return it }
let region = it.at("region", default: none)
let locale = get_locale(lang: lang, region: region)
// Only apply font if it's one of the special languages
let fonts = (
"ar": "Sakkal Majalla",
"fa": "Sakkal Majalla",
"zh-cn": "Noto Serif CJK SC",
"zh-tw": "Noto Serif CJK TC",
"ja": "Noto Serif CJK JP",
"ko": "Noto Serif CJK KR",
"hi": "Noto Serif Devanagari",
"bn": "Noto Serif Bengali",
"th": "Noto Serif Thai",
"he": "Noto Serif Hebrew",
"hy": "Noto Serif Armenian",
"ka": "Noto Serif Georgian",
"si": "Noto Serif Sinhala",
"ta": "Noto Serif Tamil",
)
if locale in fonts {
set text(font: (fonts.at(locale), "Latin Modern Roman"))
it
} else {
it
}
}
doc
}
#let localization_it = (
"Solution": "Soluzione",
"PageXofY": (count, total) => "Pagina " + str(count) + " di " + str(total),
"Subtask": it => "Subtask " + str(it),
"Day": it => "Giorno " + str(it),
"TimeLimit": "Limite di tempo",
"MemoryLimit": "Limite di memoria",
"stdin": "standard input",
"stdout": "standard output",
"Examples": "Esempi di input/output",
"Explanation": "Spiegazione",
"Implementation": "Implementazione",
"Scoring": "Assegnazione del punteggio",
"Constraints": "Assunzioni",
"points": n => if n == 1 { "punto" } else { "punti" },
"IntentionallyBlankPage": "pagina intenzionalmente vuota",
"seconds": "secondi",
"SampleGrader": "Grader di prova",
"Grader": "Grader",
"InputFormat": "Formato di input",
"OutputFormat": "Formato di output",
)
#let localization_en = (
"Solution": "Solution",
"PageXofY": (count, total) => "Page " + str(count) + " of " + str(total),
"Subtask": it => "Subtask " + str(it),
"Day": it => "Day " + str(it),
"TimeLimit": "Time limit",
"MemoryLimit": "Memory limit",
"stdin": "standard input",
"stdout": "standard output",
"Examples": "Examples",
"Explanation": "Explanation",
"Implementation": "Implementation",
"Scoring": "Scoring",
"Constraints": "Constraints",
"points": n => if n == 1 { "point" } else { "points" },
"IntentionallyBlankPage": "This page is intentionally left blank",
"seconds": "seconds",
"SampleGrader": "Sample Grader",
"Grader": "Grader",
"InputFormat": "Input format",
"OutputFormat": "Output format",
)
// Section names, with translations
#let localize(lang, string) = {
let override = override_localization.get()
let locale = get_locale(lang: lang)
if (
type(override) == dictionary and override.at(string, default: none) != none
) {
override.at(string)
} else if locale == "it" {
localization_it.at(string)
} else if locale == "en" {
localization_en.at(string)
} else {
import "locale/" + locale + ".typ": localization
localization.at(string)
}
}
#let implementation = context [ == #localize(
text.lang,
"Implementation",
) <implementation> ]
#let samplegrader = context [ == #localize(
text.lang,
"SampleGrader",
) <samplegrader> ]
#let constraints = context [ == #localize(
text.lang,
"Constraints",
) <constraint> ]
#let scoring = context [ == #localize(text.lang, "Scoring") <scoring> ]
#let explanation = context [ == #localize(
text.lang,
"Explanation",
) <explanation> ]
#let solution = context [ == #localize(text.lang, "Solution") <solution> ]
// Main show rule for statements
#let statement(doc, title: none) = {
show math.equation.where(block: false): it => box(it)
set text(size: 11pt)
show: apply_font_rules
show: it => context {
set text(font: get_font(get_locale()))
it
}
// raw text defaults to 80% smaller, undo that.
show raw: set text(size: 1.25em, font: "Latin Modern Mono")
set par(justify: true)
set list(indent: 17pt, body-indent: 7pt)
set enum(indent: 17pt, body-indent: 7pt)
show heading.where(level: 1): h => [ #h #v(7pt) ]
let contest_yaml = sys.inputs.at("contest_yaml", default: none)
let contest_yaml = if contest_yaml != none {
yaml(contest_yaml)
} else {
(logo: none)
}
let contest_description = contest_yaml.at("description", default: none)
let contest_location = contest_yaml.at("location", default: none)
let date = contest_yaml.at("date", default: none)
let logo = contest_yaml.at("logo", default: none)
let show_summary = contest_yaml.at("show_summary", default: false)
let task_yaml = yaml(sys.inputs.at("task_yaml", default: "../task.yaml"))
let name = task_yaml.at("name", default: "noname")
let title = if (title == none) [
#set text(dir: ltr)
#task_yaml.at("title", default: "No Name")
] else {
title
}
let time_limit = task_yaml.at("time_limit", default: 0)
let memory_limit = task_yaml.at("memory_limit", default: 0)
let syllabus = task_yaml.at(
"syllabuslevel",
// incoherence between tmr internal serializer and task.yaml
default: task_yaml.at("syllabus_level", default: none),
)
let syllabus_text = context {
if syllabus != none {
for _ in range(0, syllabus) {
text("🕮", fill: orange)
}
for _ in range(syllabus, 5) {
text("🕮", fill: gray)
}
}
}
let header_text = context [
#set text(size: 15pt)
#contest_description #h(1fr) #syllabus_text
#v(-7pt)
#line(length: 100%, stroke: 0.1pt)
#v(-5pt)
#set text(size: 11pt)
#{
if contest_location != none and date != none {
[#contest_location, #date]
} else {
[#contest_location #date]
}
}
#h(1fr) #strong(raw(name)) #h(3pt) #sym.circle.filled #h(3pt) #strong(raw(
upper(get_locale()),
))
]
let footer_text = context [
#set text(size: 10pt)
#line(length: 100%, stroke: 0.1pt)
#v(-5pt)
#raw(name) #h(1fr) #localize(text.lang, "PageXofY")(
here().page(),
counter(page).final().at(0),
)
]
let summary_text = context [
#h(1cm) #localize(text.lang, "TimeLimit"): $#time_limit$ #localize(text.lang, "seconds") \
#h(1cm) #localize(text.lang, "MemoryLimit"): $#memory_limit" MiB"$\
]
set page(margin: 2cm, footer: footer_text)
// Since multiple statements could be condensed into
// a single booklet, we pagebreak before every new task
pagebreak(weak: true)
if logo != none {
set text(dir: ltr)
grid(
columns: 2,
rows: 50pt,
image(logo), header_text,
column-gutter: 5pt,
align: horizon,
)
} else {
set text(dir: ltr)
block(height: 50pt, header_text)
}
heading([
#title
#[
#set text(dir: ltr)
(#raw(name))
]
])
if show_summary {
summary_text
}
doc
}
// Main show rule for editorials
#let editorial(doc) = {
show math.equation.where(block: false): it => box(it)
set text(size: 11pt)
show: apply_font_rules
show: it => context {
set text(font: get_font(get_locale()))
it
}
// raw text defaults to 80% smaller, undo that.
show raw: set text(size: 1.25em, font: "Latin Modern Mono")
set par(justify: true)
set list(indent: 17pt, body-indent: 7pt)
set enum(indent: 17pt, body-indent: 7pt)
show heading.where(level: 1): h => [ #h #v(7pt) ]
let contest_yaml = sys.inputs.at("contest_yaml", default: none)
let contest_yaml = if contest_yaml != none {
yaml(contest_yaml)
} else {
(logo: none)
}
let show_solution_contest_yaml = contest_yaml.at(
"show_solutions",
default: false,
)
let task_yaml = yaml(sys.inputs.at("task_yaml", default: "../task.yaml"))
let name = task_yaml.at("name", default: "noname")
let footer_text = context [
#set text(size: 10pt)
#line(length: 100%, stroke: 0.1pt)
#v(-5pt)
#raw(name) #h(1fr) Pagina #here().page() di #counter(page).final().at(0)
]
set page(margin: 2cm, footer: footer_text, fill: luma(200))
let show_solution = sys.inputs.at("show_solutions", default: none)
if show_solution == none and not show_solution_contest_yaml {
return
}
// Since multiple statements could be condensed into
// a single booklet, we pagebreak before every new task
pagebreak(weak: true)
solution
doc
}
#let infile = {
let file = yaml(sys.inputs.at("task_yaml", default: "../task.yaml")).at(
"infile",
default: "",
)
if file == "" { "stdin" } else { file }
}
#let outfile = {
let file = yaml(sys.inputs.at("task_yaml", default: "../task.yaml")).at(
"outfile",
default: "",
)
if file == "" { "stdout" } else { file }
}
#let task_name = yaml("../task.yaml").at("name", default: "");
// Subtask constraints and scores
#let constraint = {
let constraints = if sys.inputs.at("gen_toml", default: none) != none {
let gen_toml = sys.inputs.at("gen_toml")
let constraints = toml(gen_toml)
constraints.subtask = constraints.subtask.values()
constraints
} else {
let constraints_yaml = sys.inputs.at(
"constraints_yaml",
default: "../gen/constraints.yaml",
)
let constraints = yaml(constraints_yaml)
constraints.subtask = constraints.subtask.slice(1)
constraints
}
for (name, constraint) in constraints.pairs() {
if type(constraint) == int and constraint >= 10000 {
constraints.insert(name, num(constraint, thousandsep: "#h(0.133333em)"))
}
}
for (i, subtask) in constraints.subtask.enumerate() {
for (name, constraint) in subtask.pairs() {
if type(constraint) == int and constraint >= 10000 {
subtask.insert(name, num(constraint, thousandsep: "#h(0.133333em)"))
}
}
constraints.subtask.at(i) = subtask
}
constraints
}
#let subtask_scores = {
if sys.inputs.at("gen_toml", default: none) != none {
let gen_toml = sys.inputs.at("gen_toml")
let constraints = toml(gen_toml)
constraints.subtask.values().map(l => l.score)
} else {
let gen_gen = sys.inputs.at("gen_gen", default: "../gen/GEN")
let gen_gen = read(gen_gen)
let lines = gen_gen.split("\n")
let scores = lines
.filter(l => l.starts-with("#ST:"))
.map(l => int(l.slice(5).trim()))
assert.eq(
scores.len(),
constraint.subtask.len(),
)
scores
}
}
#let subtasks(subtask_descriptions, index_start: 0) = {
assert.eq(
subtask_scores.len(),
subtask_descriptions.len(),
)
let indices = array.range(subtask_scores.len())
context {
let st = localize(text.lang, "Subtask")
let max_subtask_idx_len = indices
.map(idx => {
measure([*#{ st(idx + index_start) }*]).width
})
.sorted()
.last()
let max_subtask_score_len = subtask_scores
.map(score => {
measure([*#score*]).width
})
.sorted()
.last()
let max_points_len = subtask_scores
.map(score => {
measure(strong(localize(text.lang, "points")(score))).width
})
.sorted()
.last()
let subtasks = array
.zip(
constraint.subtask,
subtask_scores,
subtask_descriptions,
indices,
)
.map(((cnst, score, desc, index)) => {
for (key, value) in constraint {
if key != "subtask" and cnst.at(key, default: none) == none {
cnst.insert(key, value)
}
}
let description = desc(cnst)
let stidx = box(width: max_subtask_idx_len, [#{
st(index + index_start)
}])
let score_content = [#box(width: max_subtask_score_len, [#h(
1fr,
) #score])]
let points = [#box(width: max_points_len, [#localize(
text.lang,
"points",
)(score)])]
let subtask_intro = [#stidx [#score_content #points]]
(subtask_intro, description)
})
let max_intro_len = subtasks
.map(((intro, _)) => {
measure([*#intro*:]).width
})
.sorted()
.last()
for (intro, description) in subtasks {
[- #box(width: max_intro_len, [*#intro*:]) #description]
}
}
}
#let ois_subtasks(
subtask_descriptions,
subtask_syllabus_levels,
index_start: 1,
) = {
assert.eq(
subtask_scores.len(),
subtask_descriptions.len(),
)
assert.eq(
subtask_scores.len(),
subtask_syllabus_levels.len(),
)
let indices = array.range(subtask_scores.len())
context {
let st = localize(text.lang, "Subtask")
let subtasks = array
.zip(
constraint.subtask,
subtask_scores,
subtask_descriptions,
indices,
)
.map(((cnst, score, desc, index)) => {
for (key, value) in constraint {
if key != "subtask" and cnst.at(key, default: none) == none {
cnst.insert(key, value)
}
}
let description = desc(cnst)
let stidx = [#st(index + index_start)]
let score = [(#score #localize(text.lang, "points")(score))]
let subtask_intro = [*#stidx* #score]
(subtask_intro, description)
})
for ((intro, description), syllabus) in subtasks.zip(
subtask_syllabus_levels,
) {
[
- #grid(
columns: (1fr, 1.9fr),
rows: (1em, 1.6em),
[#intro],
grid.cell(
rowspan: 2,
[#description],
),
{
for _ in range(0, syllabus) {
text("🕮", fill: orange)
}
for _ in range(syllabus, 5) {
text("🕮", fill: gray)
}
},
)
]
}
}
}
// Useful commands
#let template_box(stroke_color, background_color, icon, content) = {
block(
width: 100%,
stroke: 2pt + stroke_color,
fill: background_color,
radius: 2pt,
inset: (left: 12pt, right: 8pt, y: 8pt),
grid(
columns: (5%, auto),
column-gutter: 10pt,
rows: 1,
align: (horizon + center, left + horizon),
icon, content,
),
)
}
#let note(content) = {
template_box(
luma(123),
none,
box(
width: 2em,
height: 2em,
place(center + horizon, dy: -0.3em, text(size: 1.8em, context if (text-dir() == ltr) {
emoji.finger.r
} else {
emoji.finger.l
}))
),
content,
)
}
#let warn(content) = {
template_box(
orange,
rgb(255, 0, 0, 50),
box(
width: 2em,
height: 2em,
[
#place(horizon + center, dy: -0.3em, text(
size: 2.5em,
emoji.warning
))
],
),
content,
)
}
#let signatures(language_signatures, highlight: true) = {
set text(dir: ltr)
if type(language_signatures) == str {
table(
columns: (1fr,),
stroke: 0.1pt + black,
fill: luma(240),
inset: 8pt,
align: (horizon),
raw(language_signatures),
)
} else {
let lang_name = (
"c": "C",
"cpp": "C++",
"cs": "C#",
"go": "Go",
"hs": "Haskell",
"java": "Java",
"js": "Javascript",
"pas": "Pascal",
"py": "Python",
"rs": "Rust",
)
table(
columns: (70pt, 1fr),
stroke: 0.1pt + black,
fill: luma(240),
inset: 8pt,
align: (center + horizon, horizon),
..for (lang, sig) in language_signatures {
(
lang_name.at(lang),
if highlight { raw(sig, lang: lang) } else { raw(sig) },
)
}
)
}
}
#let ioformat(..args) = {
let pos_args = args.pos()
let n_args = pos_args.len()
for (i, format) in pos_args.enumerate() {
let above_margin = if i == 0 { auto } else { 6pt }
let below_margin = if i == n_args - 1 { auto } else { 6pt }
show raw.where(block: true): it => block(
fill: luma(240),
stroke: 1pt + luma(220),
width: 100%,
inset: 8pt,
above: above_margin,
below: below_margin,
breakable: false,
it
)
if type(format) == str {
raw(format, block: true)
} else if type(format) == array {
raw(format.map(line => str(line)).join("\n"), block: true)
} else if type(format) == content and format.func() == raw {
if format.block {
format
} else {
raw(format.text, block: true)
}
} else {
panic("ioformat: paramater has invalid type `" + str(type(format)) + "`")
}
}
}
#let inputformat(..args) = {
context [ === #localize(text.lang, "InputFormat"):]
ioformat(..args)
}
#let outputformat(..args) = {
context [ === #localize(text.lang, "OutputFormat"):]
ioformat(..args)
}
#let examples(num, infile: infile, outfile: outfile) = {
context [== #localize(text.lang, "Examples") <examples>]
let name = yaml("../task.yaml").name
set text(dir: ltr)
table(
columns: (1fr, 1fr),
rows: num + 1,
stroke: 0.1pt + black,
align: (x, y) => {
if y == 0 { center } else { auto }
},
fill: (x, y) => {
if y == 0 { luma(240) } else { luma(250) }
},
table.header(raw(infile), raw(outfile)),
..for i in array.range(num) {
let infile = name + ".input" + str(i) + ".txt"
let outfile = name + ".output" + str(i) + ".txt"
(box(raw(read(infile))), box(raw(read(outfile))))
},
)
}
#let examples-interactive(num) = {
context [== #localize(text.lang, "Examples") <examples>]
let name = yaml("../task.yaml").name
set text(dir: ltr)
for i in array.range(num) {
let file = name + ".interaction" + str(i) + ".txt"
let lines = ()
for line in read(file).split("\n") {
if line.len() < 1 {
continue
}
if line.starts-with("<") {
if lines.len() == 0 or not lines.at(lines.len() - 1).at(1) {
lines.push(((), true))
}
} else if line.starts-with(">") {
if lines.len() == 0 or lines.at(lines.len() - 1).at(1) {
lines.push(((), false))
}
} else {
panic(
"Example "
+ str(i + 1)
+ ": line "
+ line
+ " starts with neither < nor >",
)
}
lines.at(lines.len() - 1).at(0).push(line.slice(1))
}
block(
breakable: false,
table(
columns: (1fr, 0.0fr, 1fr),
stroke: none,
row-gutter: 3pt,
align: (x, y) => {
if y == 0 { center } else { auto }
},
table.header(
context strong(localize(text.lang, "Grader")),
[],
context strong(localize(text.lang, "Solution")),
),
..for (l, is_input) in lines {
let cell = table.cell(
stroke: 0.1pt + black,
colspan: 2,
fill: luma(250),
raw(l.join("\n")),
)
let pat-col1 = luma(245)
let pat-col2 = luma(255)
let pat = tiling(
size: (15pt, 15pt),
relative: "parent",
{
place(rect(width: 100%, height: 100%, fill: pat-col1))
place(polygon(
fill: pat-col2,
(0pt, 7.5pt), (7.5pt, 0pt), (15pt, 0pt), (0pt, 15pt),
))
place(polygon(
fill: pat-col2,
(15pt, 7.5pt), (7.5pt, 15pt), (15pt, 15pt),
))
}
)
let placeholder = table.cell(fill: pat, [])
if is_input {
(cell, placeholder)
} else {
(placeholder, cell)
}
},
),
)
}
}
#if sys.inputs.at("gen_toml", default: none) != none {
let gen_toml = sys.inputs.at("gen_toml")
let constraints = toml(gen_toml)
let num = if type(constraints.samples) == int {
constraints.samples
} else {
constraints.samples.num
}
examples = (infile: infile, outfile: outfile) => examples(
num,
infile: infile,
outfile: outfile,
)
examples-interactive = () => examples-interactive(num)
}