Explain concepts on the site, and reuse their code from the CLI
/usaco/concepts is a library of techniques Claude explained: the admin
asks by topic ("help explain fenwick trees") or pastes code they could
not follow, and gets an intuitive explanation with SVG diagrams plus a
C++ snippet in contest style. Given code, the walkthrough traces that
code and the snippet is a general version of it. Anyone can read the
library; generating, rewriting and deleting are admin-only.
Each concept is committed as concepts/<slug>/: concept.md (tags in a
header comment, then a # title), snippet.cpp (first line
"// concept: <slug>") and request.md (the question, for rewrites). The
slug is Claude's to pick, so a new concept's job is keyed by an id and
returns {slug} as its result; a taken slug gets -2, -3... rather than
overwriting. Rewriting starts from the saved question and version plus
a note on what to change. The index reads concepts into Index.Concepts,
with their date from git.
internal/concept shares explain's math and diagram rules, now split out
as explain.MathRules and explain.DiagramRules; the explain prompt is
byte-for-byte unchanged. Code fences in explanations are highlighted
with the same highlight.js setup as CodeBlock (web/src/highlight.js).
`usaco snippet` lists concepts, prints one's snippet, or pastes it into
a solution above main (or class Solution), keeping a .bak and refusing
a concept the file already has. In interactive mode it pastes into the
selection.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
// the CLI wires up, tests/N.in + N.out, and optionally a .url, notes.md,
|
||||
// problem.md (the statement, pasted or fetched from the judge by the site),
|
||||
// explain.md (an explanation the site had Claude write) and reviews/<id>.md
|
||||
// (feedback on a recording of the admin explaining it). The repo is
|
||||
// (feedback on a recording of the admin explaining it). Beside them,
|
||||
// concepts/<slug>/ holds a technique the site had Claude explain
|
||||
// (internal/concept): concept.md, snippet.cpp and request.md. The repo is
|
||||
// small, so everything, file contents included, is read on each rebuild,
|
||||
// and a rebuild only happens when the clone changes.
|
||||
package index
|
||||
@@ -36,6 +38,9 @@ const (
|
||||
|
||||
var Statuses = []string{Solving, Solved, Skipped}
|
||||
|
||||
// Concepts is the directory of saved concepts, which are not problems.
|
||||
const Concepts = "concepts"
|
||||
|
||||
// MaxFileSize caps what is held for one file. Test inputs can be large and
|
||||
// nobody reads a megabyte in a browser; the rest is cut and marked.
|
||||
const MaxFileSize = 64 << 10
|
||||
@@ -110,16 +115,33 @@ type Review struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// Concept is one concepts/<slug>/. Unlike a problem, it is public whatever
|
||||
// its state: nothing in it is the judge's or the admin's own words.
|
||||
type Concept struct {
|
||||
Slug string `json:"slug"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
// Content is concept.md without its header comment and title, which the
|
||||
// page shows itself.
|
||||
Content string `json:"content,omitempty"`
|
||||
Snippet *File `json:"snippet,omitempty"`
|
||||
// Request is request.md, what was asked, for rewriting the concept.
|
||||
Request string `json:"-"`
|
||||
Updated *time.Time `json:"updated,omitempty"`
|
||||
}
|
||||
|
||||
// Index is one snapshot of the clone.
|
||||
type Index struct {
|
||||
Head string
|
||||
Built time.Time
|
||||
Problems map[string]*Problem
|
||||
Concepts map[string]*Concept
|
||||
}
|
||||
|
||||
// Build reads dir, a git work tree.
|
||||
func Build(dir string) (*Index, error) {
|
||||
idx := &Index{Built: time.Now(), Problems: map[string]*Problem{}}
|
||||
idx := &Index{Built: time.Now(), Problems: map[string]*Problem{}, Concepts: map[string]*Concept{}}
|
||||
for _, status := range Statuses {
|
||||
entries, err := os.ReadDir(filepath.Join(dir, status))
|
||||
if os.IsNotExist(err) {
|
||||
@@ -145,6 +167,9 @@ func Build(dir string) (*Index, error) {
|
||||
idx.Problems[p.Name] = p
|
||||
}
|
||||
}
|
||||
if err := readConcepts(filepath.Join(dir, Concepts), idx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
head, err := gitOut(dir, "rev-parse", "HEAD")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -306,6 +331,82 @@ func readReviews(rdir string) ([]Review, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func readConcepts(cdir string, idx *Index) error {
|
||||
entries, err := os.ReadDir(cdir)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() || strings.HasPrefix(e.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
c, err := readConcept(filepath.Join(cdir, e.Name()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s/%s: %w", Concepts, e.Name(), err)
|
||||
}
|
||||
if c != nil {
|
||||
idx.Concepts[c.Slug] = c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// conceptHead is the comment internal/concept writes as concept.md's first
|
||||
// line.
|
||||
var conceptHead = regexp.MustCompile(`^<!-- concept(?: tags=(\S*))? -->\r?\n`)
|
||||
|
||||
// readConcept returns nil for a directory with neither an explanation nor a
|
||||
// snippet: there is nothing to show.
|
||||
func readConcept(cdir string) (*Concept, error) {
|
||||
c := &Concept{Slug: filepath.Base(cdir), Title: filepath.Base(cdir), Tags: []string{}}
|
||||
md, mdErr := os.ReadFile(filepath.Join(cdir, "concept.md"))
|
||||
if mdErr != nil && !os.IsNotExist(mdErr) {
|
||||
return nil, mdErr
|
||||
}
|
||||
if snip, err := readFile(filepath.Join(cdir, "snippet.cpp"), "cpp"); err == nil {
|
||||
c.Snippet = &snip
|
||||
} else if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
if mdErr != nil && c.Snippet == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if b, err := os.ReadFile(filepath.Join(cdir, "request.md")); err == nil {
|
||||
c.Request = string(b)
|
||||
}
|
||||
|
||||
content := string(md)
|
||||
if m := conceptHead.FindStringSubmatch(content); m != nil {
|
||||
for _, t := range strings.Split(m[1], ",") {
|
||||
if t != "" {
|
||||
c.Tags = append(c.Tags, t)
|
||||
}
|
||||
}
|
||||
content = content[len(m[0]):]
|
||||
}
|
||||
// The title is the first line if it is a heading, and the summary the
|
||||
// first italic line under it that is not the byline.
|
||||
if first, rest, _ := strings.Cut(content, "\n"); strings.HasPrefix(first, "# ") {
|
||||
c.Title = strings.TrimSpace(first[2:])
|
||||
content = rest
|
||||
}
|
||||
for _, line := range strings.Split(content, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasPrefix(line, "## ") {
|
||||
break
|
||||
}
|
||||
if len(line) > 2 && line[0] == '_' && line[len(line)-1] == '_' && !strings.HasPrefix(line, "_Explained by ") {
|
||||
c.Summary = line[1 : len(line)-1]
|
||||
break
|
||||
}
|
||||
}
|
||||
c.Content = strings.TrimLeft(content, "\r\n")
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func readFile(path, lang string) (File, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
@@ -378,6 +479,7 @@ func addHistory(dir string, idx *Index) error {
|
||||
solvedFiles := map[string]map[string]bool{} // name -> paths under solved/<name>/
|
||||
var c Commit
|
||||
touched := map[string]bool{}
|
||||
touchedConcepts := map[string]bool{}
|
||||
becameSolved := map[string]bool{}
|
||||
|
||||
flush := func() {
|
||||
@@ -393,6 +495,12 @@ func addHistory(dir string, idx *Index) error {
|
||||
p.LastTouched = &d
|
||||
p.History = append(p.History, c)
|
||||
}
|
||||
for slug := range touchedConcepts {
|
||||
if cn, ok := idx.Concepts[slug]; ok {
|
||||
d := c.Date
|
||||
cn.Updated = &d
|
||||
}
|
||||
}
|
||||
for name := range becameSolved {
|
||||
if p, ok := idx.Problems[name]; ok && len(solvedFiles[name]) > 0 {
|
||||
d := c.Date
|
||||
@@ -400,6 +508,7 @@ func addHistory(dir string, idx *Index) error {
|
||||
}
|
||||
}
|
||||
touched = map[string]bool{}
|
||||
touchedConcepts = map[string]bool{}
|
||||
becameSolved = map[string]bool{}
|
||||
}
|
||||
|
||||
@@ -433,6 +542,10 @@ func addHistory(dir string, idx *Index) error {
|
||||
}
|
||||
kind, path := fields[0], fields[len(fields)-1]
|
||||
segs := strings.SplitN(path, "/", 3)
|
||||
if len(segs) == 3 && segs[0] == Concepts {
|
||||
touchedConcepts[segs[1]] = true
|
||||
continue
|
||||
}
|
||||
if len(segs) < 3 || !isStatus(segs[0]) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -76,6 +77,13 @@ func fixture(t *testing.T) string {
|
||||
write("solved/moocast/reviews/2026-09-04-100000.md", "<!-- review verdict=correct audio=moocast/2026-09-04-100000.mp3 -->\n_Reviewed by m._\n")
|
||||
write("solved/moocast/reviews/notes.md", "hand-written\n")
|
||||
commit(4, "Add a test")
|
||||
|
||||
write("concepts/fenwick-tree/concept.md", "<!-- concept tags=range-query,prefix-sums -->\n# Fenwick tree\n\n_Prefix sums with updates in O(log n)._\n\n_Explained by m on 5 September 2026._\n\n## Intuition\n\nLowbits.\n")
|
||||
write("concepts/fenwick-tree/snippet.cpp", "// concept: fenwick-tree\nstruct BIT {};\n")
|
||||
write("concepts/fenwick-tree/request.md", "help explain fenwick trees\n")
|
||||
write("concepts/hand-made/snippet.cpp", "int f();\n")
|
||||
write("concepts/empty/notes.txt", "nothing to show\n")
|
||||
commit(5, "Explain concept fenwick-tree")
|
||||
return dir
|
||||
}
|
||||
|
||||
@@ -139,6 +147,26 @@ func TestBuild(t *testing.T) {
|
||||
t.Fatalf("moocast history = %+v, want 3 commits newest first", m.History)
|
||||
}
|
||||
|
||||
if len(m.History) != 3 {
|
||||
t.Fatalf("a concept's commit touched moocast: %+v", m.History)
|
||||
}
|
||||
if len(idx.Concepts) != 2 {
|
||||
t.Fatalf("concepts = %v, want fenwick-tree and hand-made (empty has nothing to show)", idx.Concepts)
|
||||
}
|
||||
f := idx.Concepts["fenwick-tree"]
|
||||
if f.Title != "Fenwick tree" || f.Summary != "Prefix sums with updates in O(log n)." || len(f.Tags) != 2 || f.Tags[1] != "prefix-sums" {
|
||||
t.Fatalf("fenwick-tree = %+v", f)
|
||||
}
|
||||
if !strings.HasPrefix(f.Content, "_Prefix sums") || strings.Contains(f.Content, "# Fenwick") || strings.Contains(f.Content, "<!--") {
|
||||
t.Fatalf("fenwick-tree content keeps its header or title: %q", f.Content)
|
||||
}
|
||||
if f.Snippet == nil || f.Snippet.Lang != "cpp" || f.Request != "help explain fenwick trees\n" || f.Updated == nil || !f.Updated.Equal(day(5)) {
|
||||
t.Fatalf("fenwick-tree snippet/request/updated = %+v / %q / %v", f.Snippet, f.Request, f.Updated)
|
||||
}
|
||||
if h := idx.Concepts["hand-made"]; h.Title != "hand-made" || h.Content != "" || h.Snippet == nil || len(h.Tags) != 0 {
|
||||
t.Fatalf("hand-made = %+v, want the slug as title and only a snippet", h)
|
||||
}
|
||||
|
||||
w := idx.Problems["watermelon"]
|
||||
if w.Status != Skipped || w.SolvedAt != nil || w.Sample != nil {
|
||||
t.Fatalf("watermelon = %+v, want skipped, unsolved, no sample (empty .in)", w.Summary)
|
||||
|
||||
Reference in New Issue
Block a user