Files
usaco/README.md
T
adamfandClaude Opus 5.5 e821df15f4
Test and publish the site / test (push) Successful in 35s
Test and publish the site / publish (push) Successful in 29s
Import LeetCode problems with usaco new <link> / usaco daily
usaco new <leetcode-link> (alias: from) and usaco daily fetch a problem
from LeetCode's GraphQL API and write problem.md, main.cpp (template
header + LeetCode's C++ starter), a generated driver.h that runs the
solution on LeetCode-format input, and the examples as tests with the
expected outputs from the statement. leetcode.h holds ListNode/TreeNode
and the parse/print helpers; everything local sits behind #ifdef LOCAL
so main.cpp stays submittable.

reset re-fetches the starter code for LeetCode problems, the REPL's new
selects the imported problem, and usaco.zsh turns off globbing so a
pasted ?envType=... link works unquoted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 21:12:16 +01:00

148 lines
8.0 KiB
Markdown

# USACO Workspace
Practice workspace for USACO problems, managed by the `./usaco` script.
The solved problems are also published at https://adam-french.co.uk/usaco/ by the
site in `site/` (see [site/README.md](site/README.md)).
## Layout
```
template.cpp starting template for every problem
site/ the website (Go server + Vue SPA), deployed as its own container
solving/<name>/ problems in progress
solved/<name>/ finished problems
skipped/<name>/ problems set aside for later
```
The website's CLI page (`site/web/src/views/CliPage.vue`) is a condensed copy of the Commands section below; keep it in step.
Each problem dir contains `main.cpp` plus `<io>.in` / `<io>.out` files (USACO file IO is wired up automatically via `STDIO("<io>")`). Optional extras: `.url` (the problem link), `tests/N.in` + `N.out`, `notes.md` (shown on the website, usually written from there), `problem.md` (the statement, saved or fetched from the website's editor) and `explain.md` (an explanation the website had Claude write).
## Commands
### Start a problem
```
./usaco new diamond-collector
./usaco new diamond-collector diamond # if the freopen name differs
./usaco new diamond-collector --url https://usaco.org/index.php?page=viewproblem2&cpid=639
```
Creates `solving/<name>/main.cpp` from the template and an empty `.in` file, then opens it in `$EDITOR`. `--url` saves the problem link for `open`.
### Start a LeetCode problem
```
./usaco new https://leetcode.com/problems/two-sum/ # any problem link (also: usaco from <link>)
./usaco daily # today's daily question
```
Fetches the problem from LeetCode's API and writes `solving/<slug>/`:
- `problem.md`: the statement. `.url`: the link.
- `main.cpp`: the template's header, then LeetCode's C++ starter code (`class Solution { ... }`). Submit it as-is: the local-only parts sit behind `#ifdef LOCAL`.
- `driver.h`: a generated `main()` (LOCAL builds only) that reads `<slug>.in` in LeetCode's testcase format (one argument per line, cases back to back), calls your solution, and prints each answer the way LeetCode does. Design problems (`LRUCache` and similar) work too. Parsing/printing lives in `leetcode.h` at the root, which also defines `ListNode` and `TreeNode`.
- `<slug>.in` holds every example, so `usaco run` prints all their answers. `tests/N.in` + `N.out` hold one example each, with the expected output from the statement, so `usaco test` checks them.
Limits: when the statement says "any order", a correct answer in a different order shows as a failure. Problems with their own node types (`Node` in clone-graph) or a custom checker (`remove-element`) get a stub `driver.h` or no expected outputs; the importer says which. Premium problems can't be fetched. `usaco reset` on a LeetCode problem re-fetches the starter code.
**Prefix matching:** everywhere a command takes a problem name, a unique prefix works — `./usaco e diam` finds `diamond-collector` (ambiguous prefixes list the candidates).
### Build & run
```
./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 -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
```
./usaco done diamond-collector # move to solved/ and auto git commit "Solve <name>"
./usaco sync # pull edits made on the website, push your commits
./usaco skip diamond-collector # move to skipped/
./usaco undo diamond-collector # bring back to solving/ from solved/ or skipped/
```
### Website sync
The website commits too: marking a problem solved/skipped, or editing its link or notes there, pushes a commit to Gitea. So `git pull` before you push — `./usaco sync` (alias `sy`) does `git pull --rebase --autostash && git push`. Each push reaches the site within seconds through a Gitea webhook.
### Edit
```
./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 (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
```
### Snippets
```
./usaco cat diamond-collector # print main.cpp to stdout (pipeable)
./usaco cat diam | wl-copy # e.g. pipe it wherever you want
./usaco copy diamond-collector # copy main.cpp to the clipboard (alias: cp)
```
`copy` auto-detects the clipboard tool: `wl-copy` on Wayland, then `xclip`/`xsel` on X11, and falls back to an OSC52 terminal escape that works even over SSH.
### Interactive mode
```
./usaco i # (aliases: interactive, repl)
```
A REPL that remembers the problem you're working on, so commands drop the name:
```
usaco [none]> s diamond-collector # select (a bare name/prefix works too)
usaco [diamond-collector:solving]> r # run, test, edit... no name needed
usaco [diamond-collector:solving]> t c # create a test case
usaco [diamond-collector:solving]> e watermelon # giving a name switches to it
usaco [watermelon:solving]> done
usaco [watermelon:solved]> none # clear the selection
usaco [none]> q
```
REPL-only commands: `s [name]` (select/show), `none`, `push [name]`/`pop`/`stack` (stash the current problem, work on another, come back), `help [cmd]`, `!cmd` (shell command run in the problem dir), `q`.
The selection persists across sessions in `.current` (cleared automatically if the problem no longer exists); history is in `.usaco_history`. Tab completion works for both commands and problem names.
### Misc
```
./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 <name> <new>' 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
```
## Shell integration (recommended)
Add to `~/.zshrc`:
```
source ~/Coding/USACO/usaco.zsh
```
This gives you:
- `usaco` as a global command (no `./` needed, works from any directory), with globbing off for its arguments so a pasted `...?envType=...` link works unquoted
- tab completion for commands and problem names (`usaco e diam<TAB>`)
- `ucd <name>` — cd straight into a problem's directory
## Typical session
```
./usaco new the-lost-cow --url <link>
./usaco open the-lost-cow # read the problem
# paste sample input into solving/the-lost-cow/the-lost-cow.in
# edit solving/the-lost-cow/main.cpp
./usaco run the-lost-cow # iterate until the output is right
./usaco done the-lost-cow # 🎉 (auto-commits)
./usaco sync # publish it on the website
```