# 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