# cp -- competitive programming aliases and functions
# Sources: cf-tool, online-judge-tools, community CP setups
CPTPL="$HOME/.local/share/templates/cp"
# ============================================================================
# COMPILATION
# ============================================================================
alias cpsane='g++ -std=c++17 -O2 -Wall -Wextra -Wshadow -Wno-unused-result'
alias cpasan='g++ -std=c++17 -g -O0 -Wall -Wextra -Wshadow -fsanitize=address,undefined -D_GLIBCXX_DEBUG -DLOCAL'
alias cpfast='g++ -std=c++17 -O2 -DLOCAL'
alias cp20='g++ -std=c++20 -O2 -Wall -Wextra -Wshadow'
alias cpjava='javac -encoding UTF-8'
alias cppy='python3'
alias cpr='g++ -std=c++17 -O2 -Wall -Wextra'
alias cpd='g++ -std=c++17 -g -O0 -Wall -Wextra -Wpedantic -fsanitize=address,undefined -D_GLIBCXX_DEBUG'
alias cpf='g++ -std=c++17 -O2 -Wall'
# ============================================================================
# BUILD & RUN SHORTCUTS
# ============================================================================
alias cra='g++ -std=c++17 -O2 -o a a.cpp && ./a'
alias crb='g++ -std=c++17 -O2 -o b b.cpp && ./b'
alias crc='g++ -std=c++17 -O2 -o c c.cpp && ./c'
alias crd='g++ -std=c++17 -O2 -o d d.cpp && ./d'
alias cre='g++ -std=c++17 -O2 -o e e.cpp && ./e'
alias crf='g++ -std=c++17 -O2 -o f f.cpp && ./f'
# ============================================================================
# CF-TOOL (CODEFORCES CLI)
# ============================================================================
alias cfl='cf list'
alias cfs='cf submit'
alias cft='cf test'
alias cfp='cf parse'
alias cfw='cf watch'
alias cfst='cf stand'
alias cfr='cf race'
# ============================================================================
# ONLINE-JUDGE-TOOLS (oj)
# ============================================================================
alias ojd='oj download'
alias ojt='oj test'
alias ojs='oj submit'
alias ojg='oj generate-input'
# ============================================================================
# ATCODER CLI (acc)
# ============================================================================
alias accn='acc new'
alias accs='acc submit'
alias acct='acc test'
# ============================================================================
# TESTING
# ============================================================================
alias timeout5='timeout 5'
alias timeout10='timeout 10'
# ============================================================================
# DIRECTORY SHORTCUTS
# ============================================================================
alias cdcp='cd ~/cp'
alias cdcf='cd ~/cp/codeforces'
alias cdac='cd ~/cp/atcoder'
alias cdlc='cd ~/cp/leetcode'
alias cdusaco='cd ~/cp/usaco'
# ============================================================================
# FUNCTIONS
# ============================================================================
# initialize Codeforces contest workspace
cfinit() {
contest="$1"
[ -z "$contest" ] && { echo "Usage: cfinit <contest-number>"; return 1; }
dir="$HOME/cp/codeforces/$contest"
mkdir -p "$dir"
for prob in a b c d e f; do
cp "$CPTPL/cpp.cpp" "$dir/$prob.cpp" 2>/dev/null || \
cat > "$dir/$prob.cpp" << 'CPPEOF'
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}
CPPEOF
done
echo "[+] CF $contest workspace: $dir"
cd "$dir"
}
# initialize AtCoder contest workspace
acinit() {
contest="$1"
[ -z "$contest" ] && { echo "Usage: acinit <contest-id> (e.g. abc300)"; return 1; }
dir="$HOME/cp/atcoder/$contest"
mkdir -p "$dir"
for prob in a b c d e f g; do
cp "$CPTPL/cpp.cpp" "$dir/$prob.cpp" 2>/dev/null || \
cat > "$dir/$prob.cpp" << 'CPPEOF'
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}
CPPEOF
done
echo "[+] AtCoder $contest workspace: $dir"
cd "$dir"
}
# initialize LeetCode problem workspace
lcinit() {
num="$1"; name="$2"
[ -z "$num" ] && { echo "Usage: lcinit <number> [name]"; return 1; }
slug="${name:-problem}"
dir="$HOME/cp/leetcode/${num}-${slug}"
mkdir -p "$dir"
cp "$CPTPL/cpp.cpp" "$dir/sol.cpp" 2>/dev/null
echo "[+] LC $num workspace: $dir"
cd "$dir"
}
# generic problem workspace
cpinit() {
name="$1"
[ -z "$name" ] && { echo "Usage: cpinit <name>"; return 1; }
cp "$CPTPL/cpp.cpp" "$name.cpp" 2>/dev/null || \
cat > "$name.cpp" << 'CPPEOF'
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}
CPPEOF
echo "[+] Created $name.cpp"
}
# compile and run with input from file or stdin
cprun() {
src="$1"; input="$2"
[ -z "$src" ] && { echo "Usage: cprun <source.cpp> [input-file]"; return 1; }
exe="${src%.cpp}"
g++ -std=c++17 -O2 -Wall -Wextra -o "$exe" "$src" || return 1
if [ -n "$input" ] && [ -f "$input" ]; then
./"$exe" < "$input"
else
./"$exe"
fi
}
# compile with debug flags and run
cpdebug() {
src="$1"; input="$2"
[ -z "$src" ] && { echo "Usage: cpdebug <source.cpp> [input-file]"; return 1; }
exe="${src%.cpp}"
g++ -std=c++17 -g -O0 -Wall -Wextra -Wshadow -fsanitize=address,undefined -D_GLIBCXX_DEBUG -DLOCAL -o "$exe" "$src" || return 1
if [ -n "$input" ] && [ -f "$input" ]; then
./"$exe" < "$input"
else
./"$exe"
fi
}
# test solution against sample inputs/outputs
cptest() {
src="$1"
[ -z "$src" ] && { echo "Usage: cptest <source.cpp>"; return 1; }
exe="${src%.cpp}"
g++ -std=c++17 -O2 -o "$exe" "$src" || return 1
pass=0; fail=0; total=0
for infile in in*.txt; do
[ ! -f "$infile" ] && continue
num=$(echo "$infile" | grep -oE '[0-9]+')
outfile="out${num}.txt"
[ ! -f "$outfile" ] && continue
total=$((total + 1))
actual=$(./"$exe" < "$infile" 2>/dev/null)
expected=$(cat "$outfile")
if [ "$actual" = "$expected" ]; then
echo "[PASS] Test $num"
pass=$((pass + 1))
else
echo "[FAIL] Test $num"
echo " Expected: $expected"
echo " Got: $actual"
fail=$((fail + 1))
fi
done
echo ""
echo "Results: $pass/$total passed, $fail failed"
}
# stress test: compare brute force vs optimized solution
cpstress() {
brute="$1"; opt="$2"; gen="$3"; iters="${4:-1000}"
[ -z "$gen" ] && { echo "Usage: cpstress <brute.cpp> <optimized.cpp> <generator.cpp> [iterations]"; return 1; }
echo "[*] Compiling..."
g++ -std=c++17 -O2 -o brute "$brute" || return 1
g++ -std=c++17 -O2 -o opt "$opt" || return 1
g++ -std=c++17 -O2 -o gen "$gen" || return 1
echo "[*] Stress testing ($iters iterations)..."
i=1
while [ "$i" -le "$iters" ]; do
./gen "$i" > stress_input.txt
brute_out=$(./brute < stress_input.txt 2>/dev/null)
opt_out=$(./opt < stress_input.txt 2>/dev/null)
if [ "$brute_out" != "$opt_out" ]; then
echo "[FAIL] Iteration $i"
echo "Input:"
cat stress_input.txt
echo "Brute: $brute_out"
echo "Opt: $opt_out"
return 1
fi
[ $((i % 100)) -eq 0 ] && echo "[*] $i iterations passed"
i=$((i + 1))
done
echo "[+] All $iters iterations passed"
rm -f brute opt gen stress_input.txt
}
# TLE checker: run with timeout
cptle() {
src="$1"; input="$2"; limit="${3:-5}"
[ -z "$src" ] && { echo "Usage: cptle <source.cpp> <input-file> [time-limit-sec]"; return 1; }
exe="${src%.cpp}"
g++ -std=c++17 -O2 -o "$exe" "$src" || return 1
start=$(date +%s%N)
timeout "$limit" ./"$exe" < "$input" > /dev/null 2>&1
status=$?
end=$(date +%s%N)
elapsed=$(( (end - start) / 1000000 ))
if [ "$status" -eq 124 ]; then
echo "[TLE] Exceeded ${limit}s limit (ran ${elapsed}ms)"
else
echo "[OK] ${elapsed}ms"
fi
}
# create a random test generator
cpgen() {
output="${1:-gen.cpp}"
cat > "$output" << 'GENEOF'
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
mt19937 rng(atoi(argv[1]));
auto rand = [&](int lo, int hi) { return uniform_int_distribution<int>(lo, hi)(rng); };
int n = rand(1, 10);
cout << n << "\n";
for (int i = 0; i < n; i++) {
cout << rand(1, 100) << " \n"[i == n-1];
}
return 0;
}
GENEOF
echo "[+] Generator template: $output"
}
# test interactive problem
cpinteractive() {
sol="$1"; interactor="$2"
[ -z "$interactor" ] && { echo "Usage: cpinteractive <solution.cpp> <interactor.cpp>"; return 1; }
g++ -std=c++17 -O2 -o sol_exe "$sol" || return 1
g++ -std=c++17 -O2 -o int_exe "$interactor" || return 1
mkfifo pipe1 pipe2 2>/dev/null
./int_exe < pipe1 > pipe2 &
./sol_exe < pipe2 > pipe1
rm -f pipe1 pipe2 sol_exe int_exe
}
# benchmark a solution
cpbench() {
src="$1"; input="$2"; runs="${3:-10}"
[ -z "$input" ] && { echo "Usage: cpbench <source.cpp> <input-file> [runs]"; return 1; }
exe="${src%.cpp}"
g++ -std=c++17 -O2 -o "$exe" "$src" || return 1
echo "[*] Benchmarking $runs runs..."
total=0
i=1
while [ "$i" -le "$runs" ]; do
start=$(date +%s%N)
./"$exe" < "$input" > /dev/null 2>&1
end=$(date +%s%N)
ms=$(( (end - start) / 1000000 ))
total=$((total + ms))
i=$((i + 1))
done
avg=$((total / runs))
echo "[+] Average: ${avg}ms over $runs runs"
}
# copy template to current directory
cptemplate() {
lang="${1:-cpp}"; name="${2:-sol}"
case "$lang" in
cpp) cp "$CPTPL/cpp.cpp" "$name.cpp" 2>/dev/null ;;
py) cp "$CPTPL/python.py" "$name.py" 2>/dev/null ;;
*) echo "Unknown lang: $lang (try cpp, py)"; return 1 ;;
esac
echo "[+] Created $name.$lang"
}
# download and save test cases manually
cpsave() {
num="$1"
[ -z "$num" ] && { echo "Usage: cpsave <test-number>"; return 1; }
echo "Paste input (Ctrl+D when done):"
cat > "in${num}.txt"
echo "Paste expected output (Ctrl+D when done):"
cat > "out${num}.txt"
echo "[+] Saved in${num}.txt and out${num}.txt"
}