Separate lib, new problems and script for web

This commit is contained in:
2026-08-26 22:25:19 +01:00
parent 6fd6d70b04
commit afdab7d4bd
44 changed files with 1060 additions and 211 deletions
+5
View File
@@ -0,0 +1,5 @@
# usaco build <name>
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
build_problem "$dir"
echo "Built $dir/main"
+6
View File
@@ -0,0 +1,6 @@
# usaco clean
for s in solving solved skipped; do
[[ -d "$ROOT/$s" ]] || continue
find "$ROOT/$s" -mindepth 2 -maxdepth 2 -type f \
\( -name main -o -name a.out -o -name '*.bak' \) -print -delete | sed "s|^$ROOT/|removed |"
done
+11
View File
@@ -0,0 +1,11 @@
# usaco done <name>
[[ $# -ge 1 ]] || usage
name="$1"
[[ -d "$ROOT/solving/$name" ]] || { echo "Error: solving/$name not found" >&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 🎉"
if git -C "$ROOT" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
git -C "$ROOT" add -A "solved/$name" "solving/$name" 2>/dev/null || true
git -C "$ROOT" commit -m "Solve $name" >/dev/null && echo "Committed: Solve $name"
fi
+5
View File
@@ -0,0 +1,5 @@
# usaco vim/edit/e <name>
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
# run from inside the dir so :!./main etc. work and .in files are at hand
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
+29
View File
@@ -0,0 +1,29 @@
# usaco io <name> [io-name]
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
old="$(detect_io "$dir" || true)"
if [[ $# -lt 2 ]]; then
stdio="$(grep -o 'STDIO("[^"]*")' "$dir/main.cpp" 2>/dev/null | sed 's/STDIO("\(.*\)")/\1/' | head -1 || true)"
echo "files: ${old:-none}"
echo "STDIO: ${stdio:-none}"
exit 0
fi
io="$2"
# rename data files
if [[ -n "$old" && "$old" != "$io" ]]; then
for ext in in out; do
[[ -e "$dir/$old.$ext" ]] && mv "$dir/$old.$ext" "$dir/$io.$ext" && echo "Renamed $old.$ext -> $io.$ext"
done
fi
[[ -e "$dir/$io.in" ]] || { touch "$dir/$io.in"; echo "Created $io.in"; }
# rewire STDIO in main.cpp
if grep -q 'STDIO("' "$dir/main.cpp" 2>/dev/null; then
sed -i "s/STDIO(\"[^\"]*\")/STDIO(\"$io\")/" "$dir/main.cpp"
echo "Updated STDIO(\"$io\") in main.cpp"
elif grep -q '^int main() {$' "$dir/main.cpp" 2>/dev/null; then
sed -i "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$dir/main.cpp"
echo "Inserted STDIO(\"$io\") in main.cpp"
else
echo "Warning: couldn't find STDIO(...) or 'int main() {' in main.cpp — wire it up manually" >&2
fi
echo "io name for $(basename "$dir") is now '$io'"
+19
View File
@@ -0,0 +1,19 @@
# usaco list
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")"
done | sort -n | cut -f2 | sed 's/^/ /'
}
echo "== Solving =="
list_by_mtime "$ROOT/solving"
echo
echo "== Solved =="
list_by_mtime "$ROOT/solved"
if [[ -d "$ROOT/skipped" && -n "$(ls -A "$ROOT/skipped" 2>/dev/null)" ]]; then
echo
echo "== Skipped =="
list_by_mtime "$ROOT/skipped"
fi
+21
View File
@@ -0,0 +1,21 @@
# usaco new <name> [io-name] [--url <link>]
[[ $# -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
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"
echo "Created solving/$name (io: $io.in / $io.out)"
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
+6
View File
@@ -0,0 +1,6 @@
# usaco open <name>
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
[[ -f "$dir/.url" ]] || { echo "Error: no URL saved for '$1' (use --url with 'new', or write $dir/.url)" >&2; exit 1; }
xdg-open "$(cat "$dir/.url")" >/dev/null 2>&1 &
echo "Opening $(cat "$dir/.url")"
+3
View File
@@ -0,0 +1,3 @@
# usaco path <name>
[[ $# -ge 1 ]] || usage
find_problem "$1"
+15
View File
@@ -0,0 +1,15 @@
# usaco reset/rm/delete <name>
[[ $# -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; }
[[ -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"
echo "Reset $name (old solution in main.cpp.bak)"
+12
View File
@@ -0,0 +1,12 @@
# usaco run <name>
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
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"
fi
+8
View File
@@ -0,0 +1,8 @@
# usaco skip <name>
[[ $# -ge 1 ]] || usage
name="$1"
[[ -d "$ROOT/solving/$name" ]] || { echo "Error: solving/$name not found" >&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"
echo "Moved to skipped/$name (usaco undo $name to bring it back)"
+20
View File
@@ -0,0 +1,20 @@
# usaco stats
count() { [[ -d "$1" ]] && find "$1" -mindepth 1 -maxdepth 1 -type d | wc -l || echo 0; }
solved=$(count "$ROOT/solved"); solving=$(count "$ROOT/solving"); skipped=$(count "$ROOT/skipped")
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)"
[[ -z "$t" ]] && continue
age=$(( now - t ))
(( age <= 7*86400 )) && week=$((week+1))
(( age <= 30*86400 )) && month=$((month+1))
done
fi
echo "Solved: $solved"
echo "Solving: $solving"
echo "Skipped: $skipped"
echo
echo "Solved in last 7 days: $week"
echo "Solved in last 30 days: $month"
+6
View File
@@ -0,0 +1,6 @@
# usaco template/tpl <name>
[[ $# -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"
+107
View File
@@ -0,0 +1,107 @@
# usaco test <name> [nums...] | usaco test create <name>
norm() { # trim trailing whitespace and trailing blank lines so diffs aren't pedantic
sed 's/[[:space:]]*$//' "$1" | awk 'NF{for(i=0;i<b;i++)print ""; b=0; print; next}{b++}'
}
test_create() { # $1 = problem dir, $2 = io basename
local dir="$1" io="$2" n
mkdir -p "$dir/tests"
n=1; while [[ -e "$dir/tests/$n.in" ]]; do n=$((n+1)); done
if [[ -s "$dir/$io.in" ]]; then
cp "$dir/$io.in" "$dir/tests/$n.in"
echo "Case $n input: took current $io.in ($(wc -l < "$dir/tests/$n.in") lines)"
else
echo "Case $n input: paste it, then Ctrl-D on an empty line"
cat > "$dir/tests/$n.in"
fi
echo "Case $n expected output: paste it, then Ctrl-D (just Ctrl-D to skip checking)"
cat > "$dir/tests/$n.out"
if [[ ! -s "$dir/tests/$n.out" ]]; then
rm "$dir/tests/$n.out"
echo "Saved tests/$n.in (no expected output — 'usaco t' will just show what it prints)"
else
echo "Saved tests/$n.in + tests/$n.out"
fi
}
run_case() { # $1 = dir, $2 = io, $3 = tests/N.in path; returns 0 pass, 1 fail, 2 unchecked
local dir="$1" io="$2" tin="$3" n ms start exp
n="$(basename "$tin" .in)"
cp "$tin" "$dir/$io.in"
rm -f "$dir/$io.out"
start=$(date +%s%N)
if ! (cd "$dir" && ./main); then
echo "${RED}✗ case $n CRASHED (nonzero exit)${RST}"
return 1
fi
ms=$(( ($(date +%s%N) - start) / 1000000 ))
exp="$dir/tests/$n.out"
if [[ ! -e "$exp" ]]; then
echo "${YEL}• case $n${RST} ${DIM}(${ms}ms, no expected output)${RST} got:"
sed 's/^/ /' "$dir/$io.out" 2>/dev/null || echo " (no $io.out written)"
return 2
elif diff -q <(norm "$exp") <(norm "$dir/$io.out") >/dev/null 2>&1; then
echo "${GRN}✓ case $n${RST} ${DIM}(${ms}ms)${RST}"
return 0
else
echo "${RED}✗ case $n${RST} ${DIM}(${ms}ms)${RST}"
diff "$DIFF_COLOR" --label expected --label got -u \
<(norm "$exp") <(norm "$dir/$io.out") | sed 's/^/ /' | tail -n +3
return 1
fi
}
[[ $# -ge 1 ]] || usage
sub=""
case "${1:-}" in create|add|new) sub=create; shift ;; esac
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"; shift || true
# io basename = the freopen filename ("<io>.in" in the problem dir)
io="$(detect_io "$dir" || true)"
[[ -n "$io" ]] || { echo "Error: no .in file in $dir — can't tell the io name" >&2; exit 1; }
if [[ "$sub" == create ]]; then
test_create "$dir" "$io"
exit 0
fi
# run mode: usaco t <name> [case-numbers...]
shopt -s nullglob
cases=("$dir"/tests/*.in)
shopt -u nullglob
if [[ ${#cases[@]} -eq 0 ]]; then
echo "No test cases yet. Create one with: usaco t create $(basename "$dir")" >&2
exit 1
fi
if [[ $# -gt 0 ]]; then # only the requested case numbers
picked=()
for want in "$@"; do
[[ -e "$dir/tests/$want.in" ]] || { echo "Error: tests/$want.in not found" >&2; exit 1; }
picked+=("$dir/tests/$want.in")
done
cases=("${picked[@]}")
fi
build_problem "$dir"
# keep whatever's in <io>.in/.out now, restore afterwards
tmp="$(mktemp -d)"
[[ -e "$dir/$io.in" ]] && cp "$dir/$io.in" "$tmp/in.orig"
[[ -e "$dir/$io.out" ]] && cp "$dir/$io.out" "$tmp/out.orig"
restore() {
[[ -e "$tmp/in.orig" ]] && cp "$tmp/in.orig" "$dir/$io.in"
[[ -e "$tmp/out.orig" ]] && cp "$tmp/out.orig" "$dir/$io.out"
rm -rf "$tmp"
}
trap restore EXIT
pass=0; fail=0
while IFS= read -r tin; do
rc=0; run_case "$dir" "$io" "$tin" || rc=$?
case $rc in 0) pass=$((pass+1)) ;; 1) fail=$((fail+1)) ;; esac
done < <(printf '%s\n' "${cases[@]}" | sort -V)
if [[ $fail -eq 0 ]]; then
echo "── ${GRN}$pass passed, $fail failed${RST} ──"
else
echo "── $pass passed, ${RED}$fail failed${RST} ──"
fi
[[ $fail -eq 0 ]]
+10
View File
@@ -0,0 +1,10 @@
# usaco undo <name>
[[ $# -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; }
[[ -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)"
+47
View File
@@ -0,0 +1,47 @@
# 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)
find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; unique prefix ok
local name="$1" s d matches=()
for s in solving solved skipped; do
if [[ -d "$ROOT/$s/$name" ]]; then echo "$ROOT/$s/$name"; return 0; fi
done
for s in solving solved skipped; do
for d in "$ROOT/$s/$name"*/; do
[[ -d "$d" ]] && matches+=("${d%/}")
done
done
if [[ ${#matches[@]} -eq 1 ]]; then echo "${matches[0]}"; return 0; fi
if [[ ${#matches[@]} -gt 1 ]]; then
echo "Error: '$name' is ambiguous:" >&2
printf ' %s\n' "${matches[@]#$ROOT/}" >&2
return 1
fi
echo "Error: problem '$name' not found in solving/, solved/, or skipped/" >&2
return 1
}
build_problem() { # $1 = problem dir
g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
}
detect_io() { # echoes the io basename (first *.in in $1), or nothing
local f
for f in "$1"/*.in; do [[ -e "$f" ]] && basename "$f" .in && return 0; done
return 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"
}
# colors (disabled when not a tty or NO_COLOR is set)
if [[ -t 1 && -z "${NO_COLOR:-}" ]]; then
GRN=$'\e[32m' RED=$'\e[31m' YEL=$'\e[33m' DIM=$'\e[2m' RST=$'\e[0m'
DIFF_COLOR=--color=always
else
GRN='' RED='' YEL='' DIM='' RST=''
DIFF_COLOR=--color=never
fi