-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·67 lines (54 loc) · 1.45 KB
/
run
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
#!/bin/sh
# bin/run-problem, My solutions to various Leetcode puzzles.
# Copyright (C) 2024, Savio Sena <savio.sena@gmail.com>
#
# Create the necessary files to work on a new problem.
target_dir=src/bin
if [ ! -d "./$target_dir" ]; then
printf "error: you must run this script from the root of the repository.\n" >&2
exit 2
fi
if [ "$1" = "-w" ]; then
shift
WRITE_OUTPUT=1
fi
if [ "$1" = "-c" ]; then
shift
EXTRA_ARGS="--features=count-allocations"
fi
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
printf "Usage: %s [-c] <problem-name> [test-case]\n" "$(basename $0)" >&2
exit 1
fi
problem_name="$1"
shift
mkdir -p output/$problem_name
run_problem() {
input_file=$1
output_file=$2
printf "running: %s < %s > %s\n" \
"$problem_name" \
"$(basename $input_file)" \
"$(basename $output_file)" >&2
if [ -n "$WRITE_OUTPUT" ]; then
cargo run -q $EXTRA_ARGS --bin $problem_name <"$input_file" |
tee "$output_file"
else
cargo run -q $EXTRA_ARGS --bin $problem_name <"$input_file"
fi
}
if [ "$#" -eq "0" ]; then
for input_file in "input/$problem_name"/*; do
run_problem "$input_file" "output/$problem_name/$(basename $input_file)"
done
else
for test_case in "$@"; do
input_file="input/$problem_name/${test_case}.txt"
output_file="output/$problem_name/${test_case}.txt"
if [ ! -f $input_file ]; then
printf "error: file not found: %s.\n" "$input_file" >&2
exit 3
fi
run_problem "$input_file" "$output_file"
done
fi