Import LeetCode problems with usaco new <link> / usaco daily
Test and publish the site / test (push) Successful in 35s
Test and publish the site / publish (push) Successful in 29s

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]>
This commit is contained in:
2026-10-19 21:12:16 +01:00
co-authored by Claude Opus 5.5
parent 995b44a57a
commit e821df15f4
9 changed files with 813 additions and 9 deletions
+6 -3
View File
@@ -70,7 +70,7 @@ ${DIM}selection${RST}
${DIM}problem commands${RST} ${DIM}(name optional — omit it to use the selection; giving one switches to it)${RST}
$WITH_PROBLEM
${DIM}other${RST}
$NO_PROBLEM, new <name>
$NO_PROBLEM, new <name>, new <leetcode-url|daily>
help [<cmd>] this, or the dispatcher's usage line for <cmd>
!cmd run a shell command in the problem dir
q quit
@@ -135,8 +135,11 @@ while :; do
continue ;;
none|-) set_current ""; continue ;;
n|new)
[[ ${#args[@]} -ge 1 ]] || { echo "usage: n <name> [io-name] [--url <link>]" >&2; continue; }
if "$ROOT/usaco" new "${args[@]}"; then set_current "${args[0]}"; fi
[[ ${#args[@]} -ge 1 ]] || { echo "usage: n <name> [io-name] [--url <link>] | n <leetcode-url|daily>" >&2; continue; }
# new reports the name it created: a LeetCode link only becomes one after the fetch
created="$(mktemp)"
if USACO_CREATED="$created" "$ROOT/usaco" new "${args[@]}"; then set_current "$(<"$created")"; fi
rm -f "$created"
continue ;;
esac
+13 -1
View File
@@ -1,4 +1,4 @@
# usaco new <name> [io-name] [--url <link>]
# usaco new <name> [io-name] [--url <link>] | usaco new <leetcode-url|daily>
[[ $# -ge 1 ]] || usage
name=""; io=""; url=""
while [[ $# -gt 0 ]]; do
@@ -8,6 +8,17 @@ while [[ $# -gt 0 ]]; do
esac
done
[[ -n "$name" ]] || usage
# A LeetCode link (or "daily": today's daily question) imports the problem:
# statement, starter code, a generated driver, and the examples as tests.
if [[ "$name" == daily || "$name" =~ ^(https?://)?(www\.)?leetcode\.com/problems/ ]]; then
name="$(python3 -I "$ROOT/lib/leetcode.py" "$ROOT" "$name")" || exit 1
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
echo "Created solving/$name (statement in problem.md; 'usaco t $name' runs the examples)"
(cd "$ROOT/solving/$name" && exec ${EDITOR:-nvim} main.cpp)
exit
fi
io="${io:-$name}"
dir="$ROOT/solving/$name"
if [[ -d "$dir" || -d "$ROOT/solved/$name" || -d "$ROOT/skipped/$name" ]]; then
@@ -17,5 +28,6 @@ mkdir -p "$dir"
fresh_main "$dir" "$io"
touch "$dir/$io.in"
[[ -n "$url" ]] && echo "$url" > "$dir/.url"
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
echo "Created solving/$name (io: $io.in / $io.out)"
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
+10 -2
View File
@@ -6,12 +6,20 @@ io="$(detect_io "$dir" || true)"
io="${io:-$name}"
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)"
if [[ -f "$dir/driver.h" ]]; then
echo " main.cpp -> LeetCode's starter code again (old one saved as main.cpp.bak)"
else
echo " main.cpp -> re-created from template (old one saved as main.cpp.bak)"
fi
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"
if [[ -f "$dir/driver.h" ]]; then # a LeetCode import: re-fetch its starter code
python3 -I "$ROOT/lib/leetcode.py" "$ROOT" "$dir" --main-only >/dev/null || exit 1
else
fresh_main "$dir" "$io"
fi
rm -f "$dir/main" "$dir/a.out" "$dir/$io.out"
echo "Reset $name (old solution in main.cpp.bak)"