diff --git a/lib/cmd/build.sh b/lib/cmd/build.sh new file mode 100644 index 0000000..8825271 --- /dev/null +++ b/lib/cmd/build.sh @@ -0,0 +1,5 @@ +# usaco build +[[ $# -ge 1 ]] || usage +dir="$(find_problem "$1")" +build_problem "$dir" +echo "Built $dir/main" diff --git a/lib/cmd/clean.sh b/lib/cmd/clean.sh new file mode 100644 index 0000000..10e274a --- /dev/null +++ b/lib/cmd/clean.sh @@ -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 diff --git a/lib/cmd/done.sh b/lib/cmd/done.sh new file mode 100644 index 0000000..ac88831 --- /dev/null +++ b/lib/cmd/done.sh @@ -0,0 +1,11 @@ +# usaco done +[[ $# -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 diff --git a/lib/cmd/edit.sh b/lib/cmd/edit.sh new file mode 100644 index 0000000..dc328dd --- /dev/null +++ b/lib/cmd/edit.sh @@ -0,0 +1,5 @@ +# usaco vim/edit/e +[[ $# -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) diff --git a/lib/cmd/io.sh b/lib/cmd/io.sh new file mode 100644 index 0000000..b07e313 --- /dev/null +++ b/lib/cmd/io.sh @@ -0,0 +1,29 @@ +# usaco io [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'" diff --git a/lib/cmd/list.sh b/lib/cmd/list.sh new file mode 100644 index 0000000..2069d5f --- /dev/null +++ b/lib/cmd/list.sh @@ -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 diff --git a/lib/cmd/new.sh b/lib/cmd/new.sh new file mode 100644 index 0000000..3b9e00a --- /dev/null +++ b/lib/cmd/new.sh @@ -0,0 +1,21 @@ +# usaco new [io-name] [--url ] +[[ $# -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) diff --git a/lib/cmd/open.sh b/lib/cmd/open.sh new file mode 100644 index 0000000..8caf5d1 --- /dev/null +++ b/lib/cmd/open.sh @@ -0,0 +1,6 @@ +# usaco open +[[ $# -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")" diff --git a/lib/cmd/path.sh b/lib/cmd/path.sh new file mode 100644 index 0000000..865786f --- /dev/null +++ b/lib/cmd/path.sh @@ -0,0 +1,3 @@ +# usaco path +[[ $# -ge 1 ]] || usage +find_problem "$1" diff --git a/lib/cmd/reset.sh b/lib/cmd/reset.sh new file mode 100644 index 0000000..20209ad --- /dev/null +++ b/lib/cmd/reset.sh @@ -0,0 +1,15 @@ +# usaco reset/rm/delete +[[ $# -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)" diff --git a/lib/cmd/run.sh b/lib/cmd/run.sh new file mode 100644 index 0000000..2ca0690 --- /dev/null +++ b/lib/cmd/run.sh @@ -0,0 +1,12 @@ +# usaco run +[[ $# -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 diff --git a/lib/cmd/skip.sh b/lib/cmd/skip.sh new file mode 100644 index 0000000..fcd3325 --- /dev/null +++ b/lib/cmd/skip.sh @@ -0,0 +1,8 @@ +# usaco skip +[[ $# -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)" diff --git a/lib/cmd/stats.sh b/lib/cmd/stats.sh new file mode 100644 index 0000000..9b92983 --- /dev/null +++ b/lib/cmd/stats.sh @@ -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" diff --git a/lib/cmd/template.sh b/lib/cmd/template.sh new file mode 100644 index 0000000..bcb99f4 --- /dev/null +++ b/lib/cmd/template.sh @@ -0,0 +1,6 @@ +# usaco template/tpl +[[ $# -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" diff --git a/lib/cmd/test.sh b/lib/cmd/test.sh new file mode 100644 index 0000000..4ecd781 --- /dev/null +++ b/lib/cmd/test.sh @@ -0,0 +1,107 @@ +# usaco test [nums...] | usaco test create +norm() { # trim trailing whitespace and trailing blank lines so diffs aren't pedantic + sed 's/[[:space:]]*$//' "$1" | awk 'NF{for(i=0;i "$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 (".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 [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 .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 ]] diff --git a/lib/cmd/undo.sh b/lib/cmd/undo.sh new file mode 100644 index 0000000..be331f0 --- /dev/null +++ b/lib/cmd/undo.sh @@ -0,0 +1,10 @@ +# usaco undo +[[ $# -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)" diff --git a/lib/common.sh b/lib/common.sh new file mode 100644 index 0000000..15c4dd7 --- /dev/null +++ b/lib/common.sh @@ -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 diff --git a/solved/closing-the-farm/.url b/solved/closing-the-farm/.url new file mode 100644 index 0000000..e84ffa1 --- /dev/null +++ b/solved/closing-the-farm/.url @@ -0,0 +1 @@ +https://usaco.org/index.php?page=viewproblem2&cpid=644 diff --git a/solved/closing-the-farm/closing.in b/solved/closing-the-farm/closing.in new file mode 100644 index 0000000..05f5fb2 --- /dev/null +++ b/solved/closing-the-farm/closing.in @@ -0,0 +1,10 @@ +4 3 + +1 2 +2 3 +3 4 + +3 +4 +1 +2 diff --git a/solved/closing-the-farm/main.cpp b/solved/closing-the-farm/main.cpp new file mode 100644 index 0000000..161ea4d --- /dev/null +++ b/solved/closing-the-farm/main.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " + +// ----- FOR LOOPS ----- +#define F(i,L,R) for (int i = L; i < R; i++) +#define FE(i,L,R) for (int i = L; i <= R; i++) +#define FD(i,L,R) for (int i = L; i > R; i--) +#define FDE(i,L,R) for (int i = L; i >= R; i--) + +// ----- WEZ ----- +#define takeI(n) int n; cin >> n +#define takeII(n,m) int n, m; cin >> n >> m +#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k +#define takeL(n) ll n; cin >> n +#define takeS(s) string s; cin >> s +#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x +#define takeVL(v,n) vector v(n); for (auto &x : v) cin >> x +#define whileZ takeI(T); while(T--) +// +// ----- MAX MIN ----- +#define REMAX(a,b) (a)=max((a),(b)) +#define REMIN(a,b) (a)=min((a),(b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- +#define DBG(vari) cerr<<#vari<<" = "<<(vari)<()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back + +bool is_connected(map> edges) { + stack stk; + unordered_set seen; + + stk.push(edges.begin()->first); + + while (!stk.empty()) { + int node = stk.top(); stk.pop(); + if (seen.count(node)) {continue;} + seen.insert(node); + for (auto nbor : edges[node]) { + stk.push(nbor); + } + } + + return seen.size() == edges.size(); +} + +void print_edges(map> edges) { + for (auto [u, v] : edges) { + cerr << u << ": "; + for (int x : v) cerr << x << ' '; + cerr << NL; + } + cerr << NL; +} + +int main() { + STDIO("closing"); + + takeII(n,m); + + map> edges; + F(i,0,m) { + takeII(u,v); + edges[u].insert(v); + edges[v].insert(u); + } + + // Populate so length of edges will be every node + F(i,1,n) edges[i]; + + + FD(i,n,0) { + YESNO(is_connected(edges)); + + takeI(to_remove); + for (auto nbor : edges[to_remove]) { + edges[nbor].erase(to_remove); + } + edges.erase(to_remove); + } +} diff --git a/solved/closing-the-farm/tests/1.in b/solved/closing-the-farm/tests/1.in new file mode 100644 index 0000000..05f5fb2 --- /dev/null +++ b/solved/closing-the-farm/tests/1.in @@ -0,0 +1,10 @@ +4 3 + +1 2 +2 3 +3 4 + +3 +4 +1 +2 diff --git a/solved/systematic-love/main.cpp b/solved/systematic-love/main.cpp new file mode 100644 index 0000000..47415aa --- /dev/null +++ b/solved/systematic-love/main.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +using namespace std; + +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " + +// ----- FOR LOOPS ----- +#define F(i,L,R) for (int i = L; i < R; i++) +#define FE(i,L,R) for (int i = L; i <= R; i++) +#define FD(i,L,R) for (int i = L; i > R; i--) +#define FDE(i,L,R) for (int i = L; i >= R; i--) + +// ----- WEZ ----- +#define takeI(n) int n; cin >> n +#define takeII(n,m) int n, m; cin >> n >> m +#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k +#define takeL(n) ll n; cin >> n +#define takeS(s) string s; cin >> s +#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x +#define takeVL(v,n) vector v(n); for (auto &x : v) cin >> x +#define whileZ takeI(T); while(T--) +// +// ----- MAX MIN ----- +#define REMAX(a,b) (a)=max((a),(b)) +#define REMIN(a,b) (a)=min((a),(b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- +#define DBG(vari) cerr<<#vari<<" = "<<(vari)<()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back + +const int HEART_SIZE = 20; +const int HALF_SIZE = HEART_SIZE / 2; + +bool is_in_love(int x, int y); + +int main() { + + string message = " SYSTEMATIC LOVE "; + int message_indent = (HALF_SIZE - (message.length() / 8)) - 1; + + for (int y = 0; y < HEART_SIZE; ++y) { + for (int x = 0; x < HEART_SIZE; ++x) { + cout << ((is_in_love(x, y)) ? "vvvv" : " "); + + if (y== HALF_SIZE-1) { + if (x == message_indent) { + cout << message.c_str(); + x += (message.length()/4); + } + } + } + cout << endl; + } + + return 0; +} + +bool is_in_love(int x, int y) { + const float width = 2.2f; + const float height = 3.0f; + const float HEART_COEFFICIENT = 0.7f; + + float check_x = ((static_cast(x) / static_cast(HEART_SIZE)) - 0.5f) * width; + float check_y = ((static_cast(HEART_SIZE - y) / static_cast(HEART_SIZE)) - 0.4f) * height; + + float top_y = sqrt(1 - pow(check_x, 2)) + (HEART_COEFFICIENT * sqrt(abs(check_x))); + float bottom_y = -sqrt(1 - pow(check_x, 2)) + (HEART_COEFFICIENT* sqrt(abs(check_x))); + + if ((bottom_y <= check_y) && (check_y <= top_y)) { + return true; + } + else return false; +} diff --git a/solved/systematic-love/systematic-love.in b/solved/systematic-love/systematic-love.in new file mode 100644 index 0000000..e69de29 diff --git a/solved/z-algorithm/.url b/solved/z-algorithm/.url new file mode 100644 index 0000000..3b7dd7a --- /dev/null +++ b/solved/z-algorithm/.url @@ -0,0 +1 @@ +https://judge.yosupo.jp/problem/zalgorithm diff --git a/solved/z-algorithm/main.cpp b/solved/z-algorithm/main.cpp new file mode 100644 index 0000000..8549ccc --- /dev/null +++ b/solved/z-algorithm/main.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +using namespace std; + +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " +#define SP " " + +// ----- FOR LOOPS ----- +#define F(i,L,R) for (int i = L; i < R; i++) +#define FE(i,L,R) for (int i = L; i <= R; i++) +#define FD(i,L,R) for (int i = L; i > R; i--) +#define FDE(i,L,R) for (int i = L; i >= R; i--) + +// ----- WEZ ----- +#define takeI(n) int n; cin >> n +#define takeII(n,m) int n, m; cin >> n >> m +#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k +#define takeL(n) ll n; cin >> n +#define takeS(s) string s; cin >> s +#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x +#define takeVL(v,n) vector v(n); for (auto &x : v) cin >> x +#define whileZ takeI(T); while(T--) +// +// ----- MAX MIN ----- +#define REMAX(a,b) (a)=max((a),(b)) +#define REMIN(a,b) (a)=min((a),(b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- +#define DBG(vari) cerr<<#vari<<" = "<<(vari)<()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back + +int main() { + FASTIO; + takeS(s); + int n = s.size(); + + F(i,0,n) { + int val = 0; + F(j,0,s.size()-i) { + if (s[j] != s[j+i]) break; + ++val; + } + cout << val << SP; + } +} diff --git a/solved/z-algorithm/tests/1.in b/solved/z-algorithm/tests/1.in new file mode 100644 index 0000000..a647f89 --- /dev/null +++ b/solved/z-algorithm/tests/1.in @@ -0,0 +1 @@ +abcbcba diff --git a/solved/z-algorithm/tests/2.in b/solved/z-algorithm/tests/2.in new file mode 100644 index 0000000..1f4f5fe --- /dev/null +++ b/solved/z-algorithm/tests/2.in @@ -0,0 +1 @@ +mississippi diff --git a/solved/z-algorithm/tests/3.in b/solved/z-algorithm/tests/3.in new file mode 100644 index 0000000..e57708b --- /dev/null +++ b/solved/z-algorithm/tests/3.in @@ -0,0 +1 @@ +ababacaca diff --git a/solved/z-algorithm/tests/4.in b/solved/z-algorithm/tests/4.in new file mode 100644 index 0000000..dee4e90 --- /dev/null +++ b/solved/z-algorithm/tests/4.in @@ -0,0 +1,4 @@ + + +aaaaa + diff --git a/solved/z-algorithm/z-algorithm.in b/solved/z-algorithm/z-algorithm.in new file mode 100644 index 0000000..e69de29 diff --git a/solving/air-cownditioning-ii/.url b/solving/air-cownditioning-ii/.url new file mode 100644 index 0000000..fce7362 --- /dev/null +++ b/solving/air-cownditioning-ii/.url @@ -0,0 +1 @@ +https://usaco.org/index.php?page=viewproblem2&cpid=1276 diff --git a/solving/air-cownditioning-ii/air-cownditioning-ii.in b/solving/air-cownditioning-ii/air-cownditioning-ii.in new file mode 100644 index 0000000..e69de29 diff --git a/solving/air-cownditioning-ii/main.cpp b/solving/air-cownditioning-ii/main.cpp new file mode 100644 index 0000000..4ab887b --- /dev/null +++ b/solving/air-cownditioning-ii/main.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +using namespace std; + +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " + +// ----- FOR LOOPS ----- +#define F(i,L,R) for (int i = L; i < R; i++) +#define FE(i,L,R) for (int i = L; i <= R; i++) +#define FD(i,L,R) for (int i = L; i > R; i--) +#define FDE(i,L,R) for (int i = L; i >= R; i--) + +// ----- WEZ ----- +#define takeI(n) int n; cin >> n +#define takeII(n,m) int n, m; cin >> n >> m +#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k +#define takeL(n) ll n; cin >> n +#define takeS(s) string s; cin >> s +#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x +#define takeVL(v,n) vector v(n); for (auto &x : v) cin >> x +#define whileZ takeI(T); while(T--) +// +// ----- MAX MIN ----- +#define REMAX(a,b) (a)=max((a),(b)) +#define REMIN(a,b) (a)=min((a),(b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- +#define DBG(vari) cerr<<#vari<<" = "<<(vari)<()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back + +int main() { + FASTIO; + + takeII(n,m); + vector> cows(n); + F(i,0,n) { + int s,t,c; + cin >> s >> t >> c; + cows[i] = {s,t,c}; + } + + vector> conditioners(m); + F(i,0,m) { + int a,b,p,m; + cin >> a >> b >> p >> m; + conditioners[i] = {a,b,p,m}; + } + + +} diff --git a/solving/moocast/.url b/solving/moocast/.url new file mode 100644 index 0000000..e34192c --- /dev/null +++ b/solving/moocast/.url @@ -0,0 +1 @@ +https://usaco.org/index.php?page=viewproblem2&cpid=668 diff --git a/solving/moocast/main.cpp b/solving/moocast/main.cpp new file mode 100644 index 0000000..81fea58 --- /dev/null +++ b/solving/moocast/main.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +using namespace std; + +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " + +// ----- FOR LOOPS ----- +#define F(i,L,R) for (int i = L; i < R; i++) +#define FE(i,L,R) for (int i = L; i <= R; i++) +#define FD(i,L,R) for (int i = L; i > R; i--) +#define FDE(i,L,R) for (int i = L; i >= R; i--) + +// ----- WEZ ----- +#define takeI(n) int n; cin >> n +#define takeII(n,m) int n, m; cin >> n >> m +#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k +#define takeL(n) ll n; cin >> n +#define takeS(s) string s; cin >> s +#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x +#define takeVL(v,n) vector v(n); for (auto &x : v) cin >> x +#define whileZ takeI(T); while(T--) +// +// ----- MAX MIN ----- +#define REMAX(a,b) (a)=max((a),(b)) +#define REMIN(a,b) (a)=min((a),(b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- +#define DBG(vari) cerr<<#vari<<" = "<<(vari)<()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back + +int squared_dist(int x, int y, int xx, int yy) { + int xDiff = abs(xx-x); + int yDiff = abs(yy-y); + return (xDiff * xDiff + yDiff * yDiff); +} + +vector seen; +int dfs(const vector>& edges, int node) { + seen[node] = true; + int res = 1; + for (auto nbor : edges[node]) { + if (!seen[nbor]) res += dfs(edges, nbor); + } + return res; +} + +int main() { + STDIO("moocast"); + + takeI(n); + vi x(n), y(n), p(n); + F(i,0,n) { + int z; + cin >> x[i] >> y[i] >> z; + p[i] = z*z; + } + + // Convert to graph + vector> edges(n); + F(i,0,n-1) { + F(j,i+1, n) { + int sq = squared_dist(x[i], y[i], x[j], y[j]); + if (sq <= p[i] || sq <= p[j]) { + edges[i].push_back(j); + edges[j].push_back(i); + } + } + } + + seen = vector(n, false); + int ans = 0; + F(i,0,n) { + if (seen[i]) continue; + REMAX(ans, dfs(edges,i)); + } + cout << ans; + + // Find size of largest connected components + // Components connected if dist(X, Y) < p_x +} diff --git a/solving/moocast/moocast.in b/solving/moocast/moocast.in new file mode 100644 index 0000000..e69de29 diff --git a/solving/moocast/tests/1.in b/solving/moocast/tests/1.in new file mode 100644 index 0000000..d9ddc6b --- /dev/null +++ b/solving/moocast/tests/1.in @@ -0,0 +1,6 @@ +4 +1 3 5 +5 4 3 +7 2 1 +6 1 1 + diff --git a/solving/purchasing-milk/main.cpp b/solving/purchasing-milk/main.cpp index 606e76a..f17c08e 100644 --- a/solving/purchasing-milk/main.cpp +++ b/solving/purchasing-milk/main.cpp @@ -1,53 +1,98 @@ -// #include "../../debugging.h" - +#include +#include #include #include using namespace std; -void preprocessPrices(vector &prices) { - for (int i = 1; i < prices.size(); i++) { - prices[i] = min(2 * prices[i - 1], prices[i]); - } -} +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; -int cheapest(const vector &prices, long long query) { - long long sum = 0; +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " - for (int j = 0; j < prices.size(); j++) { - if (query % 2 == 1) { - sum += prices[j]; - } - query /= 2; - } +// ----- FOR LOOPS ----- +#define F(i,L,R) for (int i = L; i < R; i++) +#define FE(i,L,R) for (int i = L; i <= R; i++) +#define FD(i,L,R) for (int i = L; i > R; i--) +#define FDE(i,L,R) for (int i = L; i >= R; i--) - int over = 2; - while (query > 0) { - if (query % 2 == 1) { - sum += over * prices.back(); - } - over *= 2; - query /= 2; - } - return sum; -} +// ----- WEZ ----- +#define takeI(n) int n; cin >> n +#define takeII(n,m) int n, m; cin >> n >> m +#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k +#define takeL(n) ll n; cin >> n +#define takeS(s) string s; cin >> s +#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x +#define takeVL(v,n) vector v(n); for (auto &x : v) cin >> x +#define whileZ takeI(T); while(T--) +// +// ----- MAX MIN ----- +#define REMAX(a,b) (a)=max((a),(b)) +#define REMIN(a,b) (a)=min((a),(b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- +#define DBG(vari) cerr<<#vari<<" = "<<(vari)<()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back int main() { - int N, Q; - cin >> N >> Q; + STDIO("pails"); + takeII(n,q); - vector prices(N); - for (int i = 0; i < N; i++) { - cin >> prices[i]; + + vi a(n); + for (auto &x : a) cin >> x; + + vi querys(q); + int mx = 0; + for (auto &query : querys) { + takeI(z); + REMAX(mx,z); + query = z; } - preprocessPrices(prices); - - vector queries(Q); - for (int i = 0; i < Q; i++) { - cin >> queries[i]; + F(i,1,n) { + a[i] = 2*a[i-1] < a[i] ? 2*a[i-1] : a[i]; } + F(i,n,log2(mx)) a.push_back(2*a[i-1]); - for (int i = 0; i < Q; i++) { - cout << cheapest(prices, queries[i]) << '\n'; + while(q--) { + int cost = INT_MAX; + int running_cost = 0; + + takeI(remaining); + + while(remaining > 0) { + int low = log2(remaining); + int high = low + 1; + REMIN(cost, running_cost + a[high]); + remaining -= pow(2,low); + running_cost += a[low]; + } + REMIN(cost, running_cost); + cout << cost << NL; } } diff --git a/solving/purchasing-milk/pails.in b/solving/purchasing-milk/pails.in new file mode 100644 index 0000000..e69de29 diff --git a/solving/purchasing-milk/tests/1.in b/solving/purchasing-milk/tests/1.in new file mode 100644 index 0000000..69265f4 --- /dev/null +++ b/solving/purchasing-milk/tests/1.in @@ -0,0 +1,6 @@ +2 4 +10 15 +1 +2 +6 +7 diff --git a/solving/purchasing-milk/tests/2.in b/solving/purchasing-milk/tests/2.in new file mode 100644 index 0000000..57c7f66 --- /dev/null +++ b/solving/purchasing-milk/tests/2.in @@ -0,0 +1,13 @@ +4 10 +10 25 30 70 +1 +2 +3 +4 +5 +6 +7 +8 +15 +101 + diff --git a/usaco b/usaco index 77e39f4..2d4848f 100755 --- a/usaco +++ b/usaco @@ -9,6 +9,8 @@ # usaco done Move solving/ -> solved/ and git commit # usaco undo Move solved/ or skipped/ -> solving/ # usaco skip Move solving/ -> skipped/ +# usaco io [io-name] Show the problem's io name, or set it: renames .in/.out +# and rewires STDIO("...") in main.cpp (adds it if missing) # usaco vim Open the problem's main.cpp in $EDITOR (aliases: edit, e) # usaco open Open the problem's saved URL in a browser # usaco list List problems by status @@ -16,186 +18,45 @@ # usaco path Print the problem's directory (used by the ucd shell helper) # usaco clean Delete compiled binaries and backups from all problem dirs # usaco template Re-copy template.cpp into the problem as main.cpp (backs up old one) +# usaco reset Reset the solution: fresh main.cpp from template (STDIO wired up), +# delete binary + .out; keeps tests/ and .in (aliases: rm, delete) +# usaco test create Save a test case: uses the current .in (or lets you paste one), +# then asks for the expected output. Stored in tests/N.in + N.out +# usaco test [nums...] Build and run every saved test case (or just the given ones), +# diff against expected output, show pass/fail +# +# Short aliases: n=new r=run b=build t=test d=done u=undo sk=skip o=open +# l/ls=list st=stats p=path tpl=template e=edit=vim # # Names may be unique prefixes: 'usaco e diam' finds diamond-collector. +# +# Implementation: each command lives in lib/cmd/.sh; shared helpers in lib/common.sh. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra) -usage() { sed -n '2,21p' "$0" | sed 's/^# \{0,1\}//'; exit 1; } - -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" -} +usage() { awk 'NR>1 { if (!/^#/) exit; sub(/^# ?/,""); print }' "$0"; exit 1; } cmd="${1:-}"; shift || true case "$cmd" in - new) - [[ $# -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" - # insert STDIO(""); at the top of main() so file IO is wired up - sed "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$ROOT/template.cpp" > "$dir/main.cpp" - 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) - ;; - path) - [[ $# -ge 1 ]] || usage - find_problem "$1" - ;; - 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 - ;; - build) - [[ $# -ge 1 ]] || usage - dir="$(find_problem "$1")" - build_problem "$dir" - echo "Built $dir/main" - ;; - run) - [[ $# -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 - ;; - done) - [[ $# -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 - ;; - undo) - [[ $# -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)" - ;; - skip) - [[ $# -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)" - ;; - vim|edit|e) - [[ $# -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) - ;; - open) - [[ $# -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")" - ;; - 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 - ;; - 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" - ;; - template) - [[ $# -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" - ;; + new|n) cmd=new ;; + run|r) cmd=run ;; + build|b) cmd=build ;; + test|t) cmd=test ;; + done|d) cmd=done ;; + undo|u) cmd=undo ;; + skip|sk) cmd=skip ;; + io) cmd=io ;; + vim|edit|e) cmd=edit ;; + open|o) cmd=open ;; + list|l|ls) cmd=list ;; + stats|st) cmd=stats ;; + path|p) cmd=path ;; + clean) cmd=clean ;; + template|tpl) cmd=template ;; + reset|rm|delete) cmd=reset ;; *) usage ;; esac + +source "$ROOT/lib/common.sh" +source "$ROOT/lib/cmd/$cmd.sh" diff --git a/usaco-autologin.user.js b/usaco-autologin.user.js new file mode 100644 index 0000000..b880965 --- /dev/null +++ b/usaco-autologin.user.js @@ -0,0 +1,48 @@ +// ==UserScript== +// @name USACO Auto-Login +// @namespace usaco-autologin +// @version 1.0 +// @description Automatically signs in to usaco.org when logged out +// @match http://usaco.org/* +// @match https://usaco.org/* +// @match http://www.usaco.org/* +// @match https://www.usaco.org/* +// @grant none +// ==/UserScript== + +(function () { + "use strict"; + + // ---- Fill these in ---- + const USERNAME = "YOUR_USERNAME"; + const PASSWORD = "YOUR_PASSWORD"; + // ----------------------- + + // If a logout link exists, we're already signed in. + if (document.querySelector('a[href*="logout"]')) return; + + const uname = document.querySelector('input[name="uname"]'); + const pword = document.querySelector( + 'input[name="password"], input[name="pword"]', + ); + if (!uname || !pword) return; + + // Avoid an infinite reload loop if credentials are wrong: + // only auto-submit once per minute. + const last = sessionStorage.getItem("usacoAutoLoginAt"); + if (last && Date.now() - Number(last) < 60000) return; + sessionStorage.setItem("usacoAutoLoginAt", String(Date.now())); + + uname.value = USERNAME; + pword.value = PASSWORD; + + const form = uname.closest("form"); + if (form) { + // Prefer clicking the submit button so any onclick handlers run. + const btn = form.querySelector( + 'input[type="submit"], button[type="submit"], button', + ); + if (btn) btn.click(); + else form.submit(); + } +})(); diff --git a/usaco.zsh b/usaco.zsh index 6f67d94..af6d6e0 100644 --- a/usaco.zsh +++ b/usaco.zsh @@ -25,28 +25,53 @@ _usaco() { local -a cmds cmds=( 'new:create a problem from template' + 'n:create a problem from template' 'run:build and run main.cpp' + 'r:build and run main.cpp' 'build:build main.cpp' + 'b:build main.cpp' 'vim:open main.cpp in $EDITOR' 'edit:open main.cpp in $EDITOR' 'e:open main.cpp in $EDITOR' + 'test:run saved test cases (test create to add one)' + 't:run saved test cases' 'done:mark solved (auto-commits)' + 'd:mark solved (auto-commits)' 'undo:move back to solving' + 'u:move back to solving' 'skip:set aside for later' + 'sk:set aside for later' + 'io:show or set the freopen io name' 'open:open the saved problem URL' + 'o:open the saved problem URL' 'list:list problems by status' + 'l:list problems by status' + 'ls:list problems by status' 'stats:show solve counts' + 'st:show solve counts' 'path:print problem directory' + 'p:print problem directory' 'clean:delete binaries and backups' - 'template:reset main.cpp from template' + 'template:re-copy template.cpp as main.cpp (backs up old)' + 'tpl:re-copy template.cpp as main.cpp (backs up old)' + 'reset:fresh main.cpp from template, delete binary/.out' + 'rm:fresh main.cpp from template, delete binary/.out' + 'delete:fresh main.cpp from template, delete binary/.out' ) if (( CURRENT == 2 )); then _describe 'command' cmds elif (( CURRENT == 3 )); then case "$words[2]" in - new|list|stats|clean) ;; + new|n|list|l|ls|stats|st|clean) ;; # no problem-name argument + test|t) + local -a subs + subs=('create:save a new test case' 'add:save a new test case' 'new:save a new test case') + _describe 'subcommand' subs + _usaco_problems ;; *) _usaco_problems ;; esac + elif (( CURRENT == 4 )) && [[ "$words[2]" == (test|t) && "$words[3]" == (create|add|new) ]]; then + _usaco_problems fi } if (( $+functions[compdef] )); then