Explain concepts on the site, and reuse their code from the CLI
Test and publish the site / test (push) Successful in 1m19s
Test and publish the site / publish (push) Successful in 1m31s

/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:
2026-10-20 12:58:38 +01:00
co-authored by Claude Opus 5.5
parent d4849859df
commit ccf4e4dd74
25 changed files with 1750 additions and 33 deletions
+9 -1
View File
@@ -12,7 +12,7 @@ WITH_PROBLEM="run build test done undo skip io cat copy edit open path reset"
# commands that take no problem
NO_PROBLEM="list stats clean sync lang"
# REPL-only words (used for completion and for 'help')
REPL_WORDS="s use none push pop stack help quit exit"
REPL_WORDS="s use none push pop stack help quit exit snippet"
cur=""
[[ -r "$CURRENT_FILE" ]] && cur="$(<"$CURRENT_FILE")"
@@ -72,6 +72,8 @@ ${DIM}problem commands${RST} ${DIM}(name optional — omit it to use the selecti
${DIM}other${RST}
$NO_PROBLEM, new <name> [io] [--rust|--cpp], new <leetcode-url|daily>
lint [<name>] clippy the selection, or every rust solution
snippet [<concept> [<name>]]
list concepts, or paste one's C++ into the selection
help [<cmd>] this, or the dispatcher's usage line for <cmd>
!cmd run a shell command in the problem dir
q quit
@@ -142,6 +144,12 @@ while :; do
if USACO_CREATED="$created" "$ROOT/usaco" new "${args[@]}"; then set_current "$(<"$created")"; fi
rm -f "$created"
continue ;;
snippet|sn)
# 'snippet <concept>' pastes into the selection; a second word
# names the problem, and none lists the concepts
[[ ${#args[@]} -eq 1 && -n "$cur" ]] && args+=("$cur")
"$ROOT/usaco" snippet "${args[@]+"${args[@]}"}" || true
continue ;;
lint)
# the one command whose problem name is optional: it lints the
# selection if there is one, otherwise every rust solution
+43
View File
@@ -0,0 +1,43 @@
# usaco snippet [<concept> [<problem>]]
# Concepts are techniques the website had Claude explain, under
# concepts/<slug>/ (pull with 'usaco sync' first). Their snippet.cpp starts
# with a "// concept: <slug>" line, which is how a solution that already has
# one is recognised.
if [[ $# -eq 0 ]]; then # list them
shopt -s nullglob
found=""
for d in "$ROOT"/concepts/*/; do
d="${d%/}"; [[ -f "$d/snippet.cpp" ]] || continue
found=1
title="$(concept_title "$d")"
printf ' %-28s %s\n' "$(basename "$d")" "${title:-}"
done
[[ -n "$found" ]] || echo "No concepts yet: explain one on the website, then 'usaco sync'."
exit 0
fi
cdir="$(find_concept "$1")"
snip="$cdir/snippet.cpp"
[[ -f "$snip" ]] || { echo "Error: ${cdir#$ROOT/} has no snippet.cpp" >&2; exit 1; }
if [[ $# -eq 1 ]]; then cat "$snip"; exit 0; fi
dir="$(find_problem "$2")"
main="$(main_file "$dir")"
[[ "$main" == *.cpp ]] || { echo "Error: snippets are C++, and $(basename "$dir") is $(lang_of "$dir")" >&2; exit 1; }
marker="$(head -n 1 "$snip")"
if grep -qxF "$marker" "$main"; then
echo "$(basename "$dir") already has $(basename "$cdir")" >&2; exit 1
fi
cp "$main" "$main.bak"
# Above main (or LeetCode's class Solution), so the solution can call it;
# at the end if neither is there.
awk -v snip="$snip" '
!done && /^(int|signed) main\(|^class Solution/ {
while ((getline l < snip) > 0) print l
print ""
done = 1
}
{ print }
END {
if (!done) { print ""; while ((getline l < snip) > 0) print l }
}' "$main.bak" > "$main"
echo "Added $(basename "$cdir") to ${main#$ROOT/} (old one in $(basename "$main").bak)"
+21
View File
@@ -60,6 +60,27 @@ find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; uni
return 1
}
find_concept() { # echoes dir of concepts/<slug>; unique prefix ok
local name="$1" d matches=()
if [[ -d "$ROOT/concepts/$name" ]]; then echo "$ROOT/concepts/$name"; return 0; fi
for d in "$ROOT/concepts/$name"*/; do
[[ -d "$d" ]] && matches+=("${d%/}")
done
if [[ ${#matches[@]} -eq 1 ]]; then echo "${matches[0]}"; return 0; fi
if [[ ${#matches[@]} -gt 1 ]]; then
echo "Error: '$name' is ambiguous:" >&2
printf ' %s\n' "${matches[@]#$ROOT/}" >&2
return 1
fi
echo "Error: concept '$name' not found in concepts/ (try 'usaco sync')" >&2
return 1
}
concept_title() { # $1 = concept dir: echoes the "# " title of its concept.md, if any
[[ -f "$1/concept.md" ]] || return 0
sed -n 's/^# //p' "$1/concept.md" | head -n 1
}
# ----- rust project model (for rust-analyzer / clippy) -----
# Problems are bare main.rs files with no manifest, and rust-analyzer only does
# real analysis -- type-aware diagnostics, clippy on save -- inside a cargo