From ac66ad6b0312f16194d09e389b073d72a4c65181 Mon Sep 17 00:00:00 2001 From: Adam French Date: Wed, 14 Oct 2026 22:39:16 +0100 Subject: [PATCH] Tidy the usaco CLI: prefixes everywhere, LOCAL debug builds, git-based dates - done/skip/undo accept unique prefixes like every other command - builds define LOCAL (-I repo root): the template includes debugging.h and enables DBG/printV only locally, so main.cpp is submittable as-is - run/build/test --debug (or USACO_DEBUG=1): ASan/UBSan + _GLIBCXX_DEBUG - list/stats date problems by git history, not mtimes reset by checkouts - template/tpl is now an alias of reset (which gains -y); fixes template dropping the STDIO line - run shows .out via detect_io instead of the first *.out - debugging.h: size_t loop indices (no -Wsign-compare noise) - zsh completion for i/cat/copy; README covers test, io, reset, --debug - ignore a.out Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + README.md | 24 +++++++++++++++++++----- debugging.h | 6 +++--- lib/cmd/done.sh | 4 ++-- lib/cmd/interactive.sh | 6 +++--- lib/cmd/list.sh | 3 +-- lib/cmd/reset.sh | 14 ++++++++------ lib/cmd/run.sh | 8 ++++---- lib/cmd/skip.sh | 4 ++-- lib/cmd/stats.sh | 2 +- lib/cmd/template.sh | 6 ------ lib/cmd/undo.sh | 8 +++----- lib/common.sh | 31 +++++++++++++++++++++++++++++-- solving/mowing-the-field/main.cpp | 8 ++++++-- solving/the-lost-cow/main.cpp | 4 +++- template.cpp | 12 +++++++++++- usaco | 19 +++++++++++++------ usaco.zsh | 12 +++++++++--- 18 files changed, 118 insertions(+), 54 deletions(-) delete mode 100644 lib/cmd/template.sh diff --git a/.gitignore b/.gitignore index 5baa05a..5c76de9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ skipped/*/main .current .usaco_history .current_stack +a.out diff --git a/README.md b/README.md index 1978560..1a047fd 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,19 @@ Creates `solving//main.cpp` from the template and an empty `.in` file, the ./usaco run diamond-collector # compile + run, then print the .out file ./usaco build diamond-collector # compile only ``` -Compiles with `g++ -O2 -std=c++17 -Wall -Wextra` and runs inside the problem dir so file IO just works. Put your test input in the `.in` file first. +Compiles with `g++ -O2 -std=c++17 -Wall -Wextra -DLOCAL` and runs inside the problem dir so file IO just works. Put your test input in the `.in` file first. + +Add `--debug` to `run`, `build` or `test` (or set `USACO_DEBUG=1`) for a sanitizer build (`-fsanitize=address,undefined`, `_GLIBCXX_DEBUG`): out-of-bounds indexing, overflow and other UB crash with a stack trace instead of silently giving wrong answers. + +**Debug output:** `LOCAL` is only defined by `./usaco` builds, so the template's `#ifdef LOCAL #include "debugging.h"` (pretty-printers for vectors, maps, pairs…) and its `DBG(x)` macros vanish on the judge — submit `main.cpp` as-is. + +### Test cases +``` +./usaco test create diamond-collector # save tests/N.in (current .in, or paste) + expected N.out +./usaco test diamond-collector # build, run every saved case, diff, show pass/fail + time +./usaco test diamond-collector 2 3 # only cases 2 and 3 +``` +Diffs ignore trailing whitespace and trailing blank lines. A case with no `.out` just prints what it produced. ### Finish (or give up on) a problem ``` @@ -48,15 +60,15 @@ The website commits too: marking a problem solved/skipped, or editing its link o ### Edit ``` -./usaco vim diamond-collector # open main.cpp in $EDITOR (defaults to vim) +./usaco vim diamond-collector # open main.cpp in $EDITOR (defaults to nvim) ./usaco e diamond-collector # same thing, shorter (also: edit) ``` Opens from inside the problem dir, so the `.in`/`.out` files are right there. ### Look around ``` -./usaco list # all problems by status, oldest-touched first -./usaco stats # counts + problems solved in the last 7 / 30 days +./usaco list # all problems by status, oldest-touched first (by last commit; uncommitted work counts as newest) +./usaco stats # counts + problems solved in the last 7 / 30 days (by the commit that moved them to solved/) ./usaco open diamond-collector # open the saved problem URL in a browser ``` @@ -88,7 +100,9 @@ The selection persists across sessions in `.current` (cleared automatically if t ### Misc ``` -./usaco template diamond-collector # reset main.cpp from template (old one saved as main.cpp.bak) +./usaco reset diamond-collector # fresh main.cpp from template (old one -> main.cpp.bak), delete binary + .out + # asks first (-y skips); keeps tests/ and .in (aliases: template, rm) +./usaco io diamond-collector # show the io name; 'io ' renames .in/.out and rewires STDIO ./usaco clean # delete compiled binaries (main, a.out) and .bak files everywhere ./usaco path diamond-collector # print the problem's directory ``` diff --git a/debugging.h b/debugging.h index fcb960c..2ebc516 100644 --- a/debugging.h +++ b/debugging.h @@ -36,7 +36,7 @@ std::ostream &operator<<(std::ostream &out, const std::vector &vec) { return out; } out << '['; - for (int i = 0; i < vec.size() - 1; i++) { + for (size_t i = 0; i < vec.size() - 1; i++) { out << vec[i] << ", "; } return out << vec.back() << ']'; @@ -49,7 +49,7 @@ std::ostream &operator<<(std::ostream &out, const std::array &arr) { return out; } out << '['; - for (int i = 0; i < SZ - 1; i++) { + for (size_t i = 0; i < SZ - 1; i++) { out << arr[i] << ", "; } return out << arr.back() << ']'; @@ -62,7 +62,7 @@ std::ostream &operator<<(std::ostream &out, const std::deque &deq) { return out; } out << '['; - for (int i = 0; i < deq.size() - 1; i++) { + for (size_t i = 0; i < deq.size() - 1; i++) { out << deq[i] << ", "; } return out << deq.back() << ']'; diff --git a/lib/cmd/done.sh b/lib/cmd/done.sh index 02ecabc..c16c325 100644 --- a/lib/cmd/done.sh +++ b/lib/cmd/done.sh @@ -1,7 +1,7 @@ # usaco done [[ $# -ge 1 ]] || usage -name="$1" -[[ -d "$ROOT/solving/$name" ]] || { echo "Error: solving/$name not found" >&2; exit 1; } +dir="$(find_problem "$1")"; name="$(basename "$dir")" +[[ "$dir" == "$ROOT/solving/$name" ]] || { echo "Error: $name is not in solving/ (it's in ${dir#$ROOT/})" >&2; exit 1; } [[ -d "$ROOT/solved/$name" ]] && { echo "Error: solved/$name already exists" >&2; exit 1; } mv "$ROOT/solving/$name" "$ROOT/solved/$name" echo "Moved to solved/$name 🎉" diff --git a/lib/cmd/interactive.sh b/lib/cmd/interactive.sh index 2aa59d4..24a626c 100644 --- a/lib/cmd/interactive.sh +++ b/lib/cmd/interactive.sh @@ -8,7 +8,7 @@ STACK_FILE="$ROOT/.current_stack" HISTFILE="$ROOT/.usaco_history" # commands that take a problem name (the selection is injected when you omit it) -WITH_PROBLEM="run build test done undo skip io cat copy edit open path template reset" +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" # REPL-only words (used for completion and for 'help') @@ -144,7 +144,7 @@ while :; do case "$c" in r) c=run ;; b) c=build ;; t) c=test ;; d) c=done ;; u) c=undo ;; sk) c=skip ;; e|vim) c=edit ;; o) c=open ;; l|ls) c=list ;; st) c=stats ;; p) c=path ;; - tpl) c=template ;; rm|delete) c=reset ;; cp) c=copy ;; sy) c=sync ;; + tpl|template|rm|delete) c=reset ;; cp) c=copy ;; sy) c=sync ;; esac if [[ " $NO_PROBLEM " == *" $c "* ]]; then @@ -155,7 +155,7 @@ while :; do if [[ " $WITH_PROBLEM " == *" $c "* ]]; then # 'test create' keeps its subword first; the name goes after it pre=(); if [[ "$c" == test && "${args[0]:-}" =~ ^(create|add|new)$ ]]; then pre=("${args[0]}"); args=("${args[@]:1}"); fi - if [[ ${#args[@]} -ge 1 ]]; then # explicit name: use it and switch to it + if [[ ${#args[@]} -ge 1 && "${args[0]}" != -* ]]; then # explicit name: use it and switch to it n="$(real_name "${args[0]}")" || { find_problem "${args[0]}" >/dev/null || true; continue; } set_current "$n"; args=("${args[@]:1}") elif [[ -z "$cur" ]]; then diff --git a/lib/cmd/list.sh b/lib/cmd/list.sh index 2069d5f..bbde055 100644 --- a/lib/cmd/list.sh +++ b/lib/cmd/list.sh @@ -3,8 +3,7 @@ list_by_mtime() { # list subdirs of $1 oldest-first by last modification [[ -d "$1" ]] || return 0 for d in "$1"/*/; do [[ -d "$d" ]] || continue - # newest mtime of any file in the problem dir - printf '%s\t%s\n' "$(find "$d" -type f -printf '%T@\n' | sort -n | tail -1)" "$(basename "$d")" + printf '%s\t%s\n' "$(last_touched "${d%/}")" "$(basename "$d")" done | sort -n | cut -f2 | sed 's/^/ /' } echo "== Solving ==" diff --git a/lib/cmd/reset.sh b/lib/cmd/reset.sh index 20209ad..e437e6f 100644 --- a/lib/cmd/reset.sh +++ b/lib/cmd/reset.sh @@ -1,14 +1,16 @@ -# usaco reset/rm/delete +# usaco reset/rm/delete/template/tpl [-y] [[ $# -ge 1 ]] || usage dir="$(find_problem "$1")" name="$(basename "$dir")" io="$(detect_io "$dir" || true)" io="${io:-$name}" -echo "This resets ${dir#$ROOT/} to a fresh template:" -echo " main.cpp -> re-created from template (old one saved as main.cpp.bak)" -echo " removes compiled binary and $io.out (tests/ and $io.in are kept)" -read -r -p "Reset '$name'? [y/N] " ans -[[ "$ans" == y || "$ans" == Y ]] || { echo "Aborted."; exit 1; } +if [[ "${2:-}" != -y ]]; then + echo "This resets ${dir#$ROOT/} to a fresh template:" + echo " main.cpp -> re-created from template (old one saved as main.cpp.bak)" + echo " removes compiled binary and $io.out (tests/ and $io.in are kept)" + read -r -p "Reset '$name'? [y/N] " ans + [[ "$ans" == y || "$ans" == Y ]] || { echo "Aborted."; exit 1; } +fi [[ -f "$dir/main.cpp" ]] && cp "$dir/main.cpp" "$dir/main.cpp.bak" fresh_main "$dir" "$io" rm -f "$dir/main" "$dir/a.out" "$dir/$io.out" diff --git a/lib/cmd/run.sh b/lib/cmd/run.sh index 2ca0690..2e7690d 100644 --- a/lib/cmd/run.sh +++ b/lib/cmd/run.sh @@ -5,8 +5,8 @@ build_problem "$dir" # run from inside the dir so freopen finds the .in file (cd "$dir" && ./main) # show the .out file if the program wrote one -out="$(ls "$dir"/*.out 2>/dev/null | grep -v '/a\.out$' | head -1 || true)" -if [[ -n "$out" ]]; then - echo "── $(basename "$out") ──" - cat "$out" +io="$(detect_io "$dir" || true)" +if [[ -n "$io" && -f "$dir/$io.out" ]]; then + echo "── $io.out ──" + cat "$dir/$io.out" fi diff --git a/lib/cmd/skip.sh b/lib/cmd/skip.sh index fcd3325..92093ff 100644 --- a/lib/cmd/skip.sh +++ b/lib/cmd/skip.sh @@ -1,7 +1,7 @@ # usaco skip [[ $# -ge 1 ]] || usage -name="$1" -[[ -d "$ROOT/solving/$name" ]] || { echo "Error: solving/$name not found" >&2; exit 1; } +dir="$(find_problem "$1")"; name="$(basename "$dir")" +[[ "$dir" == "$ROOT/solving/$name" ]] || { echo "Error: $name is not in solving/ (it's in ${dir#$ROOT/})" >&2; exit 1; } [[ -d "$ROOT/skipped/$name" ]] && { echo "Error: skipped/$name already exists" >&2; exit 1; } mkdir -p "$ROOT/skipped" mv "$ROOT/solving/$name" "$ROOT/skipped/$name" diff --git a/lib/cmd/stats.sh b/lib/cmd/stats.sh index 9b92983..e717c1a 100644 --- a/lib/cmd/stats.sh +++ b/lib/cmd/stats.sh @@ -5,7 +5,7 @@ now=$(date +%s); week=0; month=0 if [[ -d "$ROOT/solved" ]]; then for d in "$ROOT/solved"/*/; do [[ -d "$d" ]] || continue - t="$(find "$d" -type f -printf '%T@\n' | sort -n | tail -1 | cut -d. -f1)" + t="$(solved_at "${d%/}")" [[ -z "$t" ]] && continue age=$(( now - t )) (( age <= 7*86400 )) && week=$((week+1)) diff --git a/lib/cmd/template.sh b/lib/cmd/template.sh deleted file mode 100644 index bcb99f4..0000000 --- a/lib/cmd/template.sh +++ /dev/null @@ -1,6 +0,0 @@ -# usaco template/tpl -[[ $# -ge 1 ]] || usage -dir="$(find_problem "$1")" -[[ -f "$dir/main.cpp" ]] && cp "$dir/main.cpp" "$dir/main.cpp.bak" && echo "Backed up old main.cpp -> main.cpp.bak" -cp "$ROOT/template.cpp" "$dir/main.cpp" -echo "Refreshed $dir/main.cpp from template" diff --git a/lib/cmd/undo.sh b/lib/cmd/undo.sh index be331f0..2a4071f 100644 --- a/lib/cmd/undo.sh +++ b/lib/cmd/undo.sh @@ -1,10 +1,8 @@ # usaco undo [[ $# -ge 1 ]] || usage -name="$1"; from="" -for s in solved skipped; do - [[ -d "$ROOT/$s/$name" ]] && from="$s" && break -done -[[ -n "$from" ]] || { echo "Error: '$name' not found in solved/ or skipped/" >&2; exit 1; } +dir="$(find_problem "$1")"; name="$(basename "$dir")" +from="$(basename "$(dirname "$dir")")" +[[ "$from" != solving ]] || { echo "Error: $name is already in solving/" >&2; exit 1; } [[ -d "$ROOT/solving/$name" ]] && { echo "Error: solving/$name already exists" >&2; exit 1; } mv "$ROOT/$from/$name" "$ROOT/solving/$name" echo "Moved back to solving/$name (was $from)" diff --git a/lib/common.sh b/lib/common.sh index 15c4dd7..016a7a7 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -1,7 +1,10 @@ # common.sh - shared helpers for usaco commands (sourced, not executed) # Expects ROOT to be set by the dispatcher. -CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra) +# -DLOCAL turns on the template's debugging.h include and DBG macros (judges don't define it) +CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra -DLOCAL -I"$ROOT") +# --debug (or USACO_DEBUG=1): catch out-of-bounds, overflow and UB at runtime +DEBUG_CXXFLAGS=(-g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -D_GLIBCXX_DEBUG) find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; unique prefix ok local name="$1" s d matches=() @@ -24,7 +27,12 @@ find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; uni } build_problem() { # $1 = problem dir - g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main" + if [[ -n "${USACO_DEBUG:-}" ]]; then + echo "${DIM}(debug build: sanitizers on)${RST}" >&2 + g++ "${CXXFLAGS[@]}" "${DEBUG_CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main" + else + g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main" + fi } detect_io() { # echoes the io basename (first *.in in $1), or nothing @@ -33,6 +41,25 @@ detect_io() { # echoes the io basename (first *.in in $1), or nothing return 1 } +last_touched() { # $1 = problem dir: epoch of its last change + # uncommitted work counts as "now"-ish (newest mtime); otherwise the last commit. + # File mtimes alone are useless here: every checkout/pull resets them. + local t + if [[ -n "$(git -C "$ROOT" status --porcelain -- "$1" 2>/dev/null)" ]]; then + find "$1" -type f -printf '%T@\n' | sort -n | tail -1 | cut -d. -f1 + elif t="$(git -C "$ROOT" log -1 --format=%ct -- "$1" 2>/dev/null)" && [[ -n "$t" ]]; then + echo "$t" + else + find "$1" -type f -printf '%T@\n' | sort -n | tail -1 | cut -d. -f1 + fi +} + +solved_at() { # $1 = solved/ dir: epoch it (last) entered solved/ + local t + t="$(git -C "$ROOT" log -1 --diff-filter=A --format=%ct -- "$1" 2>/dev/null)" + [[ -n "$t" ]] && echo "$t" || last_touched "$1" +} + fresh_main() { # $1 = problem dir, $2 = io name: main.cpp from template with STDIO wired up sed "s/^int main() {$/int main() {\n STDIO(\"$2\");/" "$ROOT/template.cpp" > "$1/main.cpp" } diff --git a/solving/mowing-the-field/main.cpp b/solving/mowing-the-field/main.cpp index ab02c42..eb10551 100644 --- a/solving/mowing-the-field/main.cpp +++ b/solving/mowing-the-field/main.cpp @@ -4,7 +4,9 @@ #include #include #include -#include "../../debugging.h" +#ifdef LOCAL +#include "debugging.h" +#endif using namespace std; // ----- FILE IO ----- @@ -81,7 +83,9 @@ int main() { } } - cerr << seen ; +#ifdef LOCAL + cerr << seen; +#endif cout << maxRes << NL; return 0; } diff --git a/solving/the-lost-cow/main.cpp b/solving/the-lost-cow/main.cpp index e8249d7..55502e5 100644 --- a/solving/the-lost-cow/main.cpp +++ b/solving/the-lost-cow/main.cpp @@ -1,4 +1,6 @@ -#include "../../debugging.h" +#ifdef LOCAL +#include "debugging.h" +#endif #include #include diff --git a/template.cpp b/template.cpp index 963caa5..fedbf67 100644 --- a/template.cpp +++ b/template.cpp @@ -1,5 +1,8 @@ #include using namespace std; +#ifdef LOCAL // set by ./usaco builds only, so this is safe to submit as-is +#include "debugging.h" +#endif // ----- TYPES ----- @@ -65,11 +68,18 @@ typedef vector vi; // ----- OUTPUT ----- #define YESNO(b) cout << ((b) ? "YES" : "NO") << NL -// ----- DEBUG ----- +// ----- DEBUG ----- (compiled out on the judge) +#ifdef LOCAL #define DBG(vari) cerr << #vari << " = " << (vari) << endl #define DBG2(a, b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl #define printA(a, L, R) FE(i, L, R) cerr << a[i] << (i == R ? '\n' : ' ') #define printV(a) printA(a, 0, SZ(a) - 1) +#else +#define DBG(vari) +#define DBG2(a, b) +#define printA(a, L, R) +#define printV(a) +#endif // ----- STL DS #define ALL(c) (c).begin(), (c).end() diff --git a/usaco b/usaco index cf1274c..5098f30 100755 --- a/usaco +++ b/usaco @@ -28,18 +28,19 @@ # usaco path Print the problem's directory (used by the ucd shell helper) # usaco clean Delete compiled binaries and backups from all problem dirs # usaco sync git pull --rebase (picks up edits made on the website), then push -# usaco template Re-copy template.cpp into the problem as main.cpp (backs up old one) -# usaco reset Reset the solution: fresh main.cpp from template (STDIO wired up), -# delete binary + .out; keeps tests/ and .in (aliases: rm, delete) +# usaco reset [-y] Reset the solution: fresh main.cpp from template (STDIO wired up, +# old one saved as main.cpp.bak), delete binary + .out; keeps tests/ +# and .in. Asks first unless -y (aliases: rm, delete, template, tpl) # usaco test create Save a test case: uses the current .in (or lets you paste one), # then asks for the expected output. Stored in tests/N.in + N.out # usaco test [nums...] Build and run every saved test case (or just the given ones), # diff against expected output, show pass/fail # # Short aliases: n=new r=run b=build t=test d=done u=undo sk=skip o=open -# l/ls=list st=stats p=path tpl=template e=edit=vim sy=sync +# l/ls=list st=stats p=path e=edit=vim sy=sync # # Names may be unique prefixes: 'usaco e diam' finds diamond-collector. +# run/build/test take --debug: build with sanitizers (-fsanitize=address,undefined). # # Implementation: each command lives in lib/cmd/.sh; shared helpers in lib/common.sh. @@ -68,10 +69,16 @@ case "$cmd" in path|p) cmd=path ;; clean) cmd=clean ;; sync|sy) cmd=sync ;; - template|tpl) cmd=template ;; - reset|rm|delete) cmd=reset ;; + reset|rm|delete|template|tpl) cmd=reset ;; *) usage ;; esac +# --debug may appear anywhere in the arguments +args=() +for a in "$@"; do + if [[ "$a" == --debug ]]; then export USACO_DEBUG=1; else args+=("$a"); fi +done +set -- "${args[@]+"${args[@]}"}" + source "$ROOT/lib/common.sh" source "$ROOT/lib/cmd/$cmd.sh" diff --git a/usaco.zsh b/usaco.zsh index 7cc5453..9b218d3 100644 --- a/usaco.zsh +++ b/usaco.zsh @@ -24,6 +24,9 @@ _usaco_problems() { _usaco() { local -a cmds cmds=( + 'i:interactive mode (REPL with a current problem)' + 'interactive:interactive mode (REPL with a current problem)' + 'repl:interactive mode (REPL with a current problem)' 'new:create a problem from template' 'n:create a problem from template' 'run:build and run main.cpp' @@ -42,6 +45,9 @@ _usaco() { 'skip:set aside for later' 'sk:set aside for later' 'io:show or set the freopen io name' + 'cat:print main.cpp to stdout' + 'copy:copy main.cpp to the clipboard' + 'cp:copy main.cpp to the clipboard' 'open:open the saved problem URL' 'o:open the saved problem URL' 'list:list problems by status' @@ -54,8 +60,8 @@ _usaco() { 'clean:delete binaries and backups' 'sync:pull website edits, push local commits' 'sy:pull website edits, push local commits' - 'template:re-copy template.cpp as main.cpp (backs up old)' - 'tpl:re-copy template.cpp as main.cpp (backs up old)' + 'template:same as reset' + 'tpl:same as reset' 'reset:fresh main.cpp from template, delete binary/.out' 'rm:fresh main.cpp from template, delete binary/.out' 'delete:fresh main.cpp from template, delete binary/.out' @@ -64,7 +70,7 @@ _usaco() { _describe 'command' cmds elif (( CURRENT == 3 )); then case "$words[2]" in - new|n|list|l|ls|stats|st|clean|sync|sy) ;; # no problem-name argument + i|interactive|repl|new|n|list|l|ls|stats|st|clean|sync|sy) ;; # no problem-name argument test|t) local -a subs subs=('create:save a new test case' 'add:save a new test case' 'new:save a new test case')