Merge pull request #1 from szdytom/main

add vhack
This commit is contained in:
Tony Zhang 2022-01-18 23:54:14 +08:00 committed by GitHub
commit 58c16ab3c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 147 additions and 23 deletions

View File

@ -18,29 +18,24 @@ def run(name: str, in_path, out_path):
def cmp_file(file_path1, file_path2): def cmp_file(file_path1, file_path2):
file1 = open(file_path1, mode="r") with open(file_path1, mode="r") as file1:
file2 = open(file_path2, mode="r") with open(file_path2, mode="r") as file2:
while True:
a = file1.readline()
b = file2.readline()
if (len(a) == 0 and len(b) != 0) or (len(a) == 0 and len(b) != 0):
return False
if len(a) == 0 and len(b) == 0:
break
a = a.split()
b = b.split()
if a != b:
return False
in_1 = "#" return True
in_2 = "#"
while not len(in_1) == 0 and not len(in_1) == 0:
in_1 = file1.readline()
in_2 = file2.readline()
while len(in_1.split()) == 0 and not len(in_1) == 0:
in_1 = file1.readline()
while len(in_2.split()) == 0 and not len(in_2) == 0:
in_2 = file2.readline()
if in_1.split() != in_2.split():
file1.close()
file2.close()
return False
file1.close()
file2.close()
return True
def check_res(code, in_path, ans_path): def check_res(code, in_path, ans_path):
@ -70,7 +65,7 @@ folder = vconf.readline().strip()
if (not os.path.exists(folder)): if (not os.path.exists(folder)):
os.makedirs(folder) os.makedirs(folder)
print("Start Checking solution for {}.".format(name)) print("Start CHECKING solution for {}.".format(name))
print() print()
sub_tasks = int(sub_tasks) sub_tasks = int(sub_tasks)

129
vhack.py Normal file
View File

@ -0,0 +1,129 @@
import subprocess
import time
import sys
import os
def run(name: str, in_path, out_path):
in_file = open(in_path, mode="r")
out_file = open(out_path, mode="w")
start_time = time.time()
code = subprocess.call(name, stdin=in_file, stdout=out_file)
end_time = time.time()
in_file.close()
out_file.close()
return (end_time - start_time, code)
def cmp_file(file_path1, file_path2):
with open(file_path1, mode="r") as file1:
with open(file_path2, mode="r") as file2:
while True:
a = file1.readline()
b = file2.readline()
if (len(a) == 0 and len(b) != 0) or (len(a) == 0 and len(b) != 0):
return False
if len(a) == 0 and len(b) == 0:
break
a = a.split()
b = b.split()
if a != b:
return False
return True
def check_res(code, in_path, ans_path):
if not code == 0:
return ("Runtime Error (return {})".format(code), 2)
if not cmp_file(in_path, ans_path):
return ("Wrong Answer", 1)
return ("Answer Correct", 0)
def check_file(file_path: str, err_msg: str):
if not os.path.exists(file_path):
print(err_msg, file=sys.stderr, flush=True)
exit(1)
return
check_file("vhack.conf", "[ERR] Configure file 'vhack.conf' not found.")
vconf = open("vhack.conf", "r")
name, type = vconf.readline().split()
print("Start HACKING data for {}.".format(name))
print()
atp = 0
while True:
atp += 1
std_path = "./std_{}".format(name)
run_path = "./run_{}".format(name)
mk_path = "./mk_{}".format(name)
in_path = ".input.tmp"
out_path = "hack.in"
ans_path = "hack.out"
fatout_path = "hack.fat"
check_file(mk_path, "[ERR] Executable file {} not found.".format(mk_path))
check_file(std_path, "[ERR] Executable file {} not found.".format(std_path))
check_file(std_path, "[ERR] Executable file {} not found.".format(run_path))
input_tmp = open(in_path, "w")
input_tmp.write(str(type))
input_tmp.close()
mk_time, mk_code = run(mk_path, in_path, out_path)
if mk_code != 0:
print(
"****[ERR] {} Runtime Error on making test-case of attemption#{}. (abort)".format(
mk_path, atp
)
)
exit(1)
std_time, std_code = run(std_path, out_path, ans_path)
if std_code != 0:
print(
"****[ERR] {} Runtime Error on making test-case of attemption#{}. (abort)".format(
std_path, atp
)
)
exit(1)
fat_time, fat_code = run(run_path, out_path, fatout_path)
if fat_code != 0:
print(
"Attemption#{}: Hack Success[{}ms --- Runtime Error]".format(
atp, round(std_time, 2)
)
)
break
if not cmp_file(ans_path, fatout_path):
print(
"Attemption#{}: Hack Success[{}ms --- {}ms]".format(
atp, round(std_time, 2), round(fat_time, 2)
)
)
break
print(
"Attemption#{}: Hack Failed[{}ms --- {}ms]".format(
atp, round(std_time, 2), round(fat_time, 2)
)
)