# common.sh - shared helpers for usaco commands (sourced, not executed) # Expects ROOT to be set by the dispatcher. # -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=() 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 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 local f for f in "$1"/*.in; do [[ -e "$f" ]] && basename "$f" .in && return 0; done 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" } # 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