~kris/dots

srice

ref: 178c9a25a484ead715392d50e21d03df486b8b41 srice/.config/.mksh/cp.sh -rw-r--r-- 10.1 KiB
178c9a25 — Kris Yotam conky: sync proper config from moirai (was the greenred variant) 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# 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"
}