# usaco snippet [ []] # Concepts are techniques the website had Claude explain, under # concepts// (pull with 'usaco sync' first). Their snippet.cpp starts # with a "// concept: " 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)"