-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudge.sh
executable file
·93 lines (72 loc) · 1.82 KB
/
judge.sh
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
#!/bin/bash
# Usage: execute submission and return verdict in json format
#
# ./judge.sh tl ml container_id input output checker execution_commands
tl=$1
shift
ml=$1
shift
container_id=$1
shift
input=$1
shift
output=$1
shift
checker=$1
shift
execution=$@
commands=$(cat << EOF
time="0"
memory="0"
verdict=""
out=\$( { /usr/bin/time -f '\n%e %M' $execution; } 2>&1 )
status=\$?
out=(\$(echo -e "\$out" | tail -n1));
time=\${out[0]}
memory=\${out[1]}
time_exceeded=\$(/usr/bin/python -c "print \$time >= $tl")
memory_exceeded=\$(/usr/bin/python -c "print \$memory >= $ml * 1000")
if [ "\$time_exceeded" == "True" ]; then
verdict="TIME_LIMIT_EXCEEDED"
elif [ "\$memory_exceeded" == "True" ]; then
verdict="MEMORY_LIMIT_EXCEEDED"
elif [ "\$status" -ne "0" ]; then
verdict="RUNTIME_ERROR"
else
diff=\$(./$checker $input $output $input".out")
if [ "\$?" -eq "0" ]; then
verdict="OK"
else
verdict="WRONG_ANSWER"
fi
fi
echo "{\n
\"test_case\" : \"$input\",\n
\"time\" : "\"\$time"s\",\n
\"memory\" : "\"\$memory"KB\",\n
\"exit_code\" : \"\$status\",\n
\"verdict\" : \"\$verdict\"\n}"
EOF
)
running=$(docker inspect --format="{{ .State.Running }}" "$container_id" 2> /dev/null)
if [ "$running" != "true" ]; then
echo "Error: container '$container_id' is not running"
exit 1
fi
out=$(timeout $tl docker exec "$container_id" bash -c "$commands" || true)
if [ "$out" == "" ]; then
file="/sys/fs/cgroup/memory/docker/$container_id/memory.usage_in_bytes"
memory_KB="0"
if [ -e $file ]; then
memory=$(cat $file)
memory_KB=$(echo "$memory / 1000" | bc)
fi
echo -e "{
\"test_case\" : \"$input\",
\"time\" : \""$tl"s\",
\"memory\" : \""$memory_KB"KB\",
\"exit_code\" : \"0\",
\"verdict\" : \"TIME_LIMIT_EXCEEDED\"\n}"
else
echo -e $out
fi