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]>
34 lines
1.3 KiB
Bash
34 lines
1.3 KiB
Bash
# usaco new <name> [io-name] [--url <link>] | usaco new <leetcode-url|daily>
|
|
[[ $# -ge 1 ]] || usage
|
|
name=""; io=""; url=""
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--url) url="${2:-}"; shift 2 ;;
|
|
*) if [[ -z "$name" ]]; then name="$1"; elif [[ -z "$io" ]]; then io="$1"; fi; shift ;;
|
|
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
|
|
echo "Error: '$name' already exists" >&2; exit 1
|
|
fi
|
|
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)
|