"""Run only INSIDE the restricted Docker container, never on the host.""" import decimal import json import os import resource import subprocess import sys def equal(actual, expected): a, b = actual.split(), expected.split() if len(a) != len(b): return False for x, y in zip(a, b): if x == y: continue try: xx, yy = decimal.Decimal(x), decimal.Decimal(y) if not (xx.is_finite() and yy.is_finite()): return False # Integer output must match exactly, without float precision loss. if all(t.lstrip("+-").isdigit() for t in (x,y)): if xx != yy: return False elif abs(xx-yy) > max(decimal.Decimal("0.000001"), abs(yy)*decimal.Decimal("0.000001")): return False except decimal.InvalidOperation: return False return True def limits(): resource.setrlimit(resource.RLIMIT_CPU, (3, 3)) resource.setrlimit(resource.RLIMIT_FSIZE, (1 << 20, 1 << 20)) def main(): if os.environ.get("SWIFT15_CODE_SANDBOX") != "1": raise SystemExit("Refusing to execute generated code outside the configured container") task = json.load(sys.stdin) with open("/tmp/solution.py", "w") as f: f.write(task["code"]) passed = 0 for test in task["tests"]: try: r = subprocess.run([sys.executable, "-I", "/tmp/solution.py"], input=test["input"], text=True, capture_output=True, timeout=4, preexec_fn=limits) if r.returncode or not equal(r.stdout, test["output"]): print(json.dumps({"correct": False, "passed": passed, "total": len(task["tests"]), "reason": "runtime_error" if r.returncode else "wrong_answer"})) return except subprocess.TimeoutExpired: print(json.dumps({"correct": False, "passed": passed, "total": len(task["tests"]), "reason": "test_timeout"})) return passed += 1 print(json.dumps({"correct": True, "passed": passed, "total": passed})) if __name__ == "__main__": main()