#!/usr/bin/env python3 """The project's judge: every candidate (*.json with "vectors") in the route's folder, checked exactly by kissing.py. Both are in checks/judge/, which a check runs at the project's approved version. usage: check.py FOLDER [DIMENSION]. A sub-problem passes its dimension, which every candidate must have; the tree's root passes none and takes all four. One line per candidate, then the best count in each dimension. It writes metrics.json here: the score is the best count for a sub-problem, and for the root the number of dimensions in which the best count reaches the record. Fails when a candidate is unreadable, malformed or invalid.""" import json import pathlib import sys from kissing import RECORD, CandidateError, vectors, verify root = pathlib.Path(sys.argv[1]) dims = (int(sys.argv[2]),) if len(sys.argv) > 2 else tuple(RECORD) best, bad = {}, 0 for path in sorted(root.rglob("*.json")) if root.exists() else []: name = path.relative_to(root) try: candidate = json.loads(path.read_text()) except Exception as exc: print(f"{name}: unreadable ({exc})") bad += 1 continue if not isinstance(candidate, dict) or "vectors" not in candidate: continue try: d, vs = vectors(candidate, dims) verify(vs) except CandidateError as exc: print(f"{name}: invalid: {exc}") bad += 1 continue print(f"d={d}: {len(vs)} vectors ({name})") if len(vs) > best.get(d, (0,))[0]: best[d] = (len(vs), name) if best: reached = sum(best[d][0] >= RECORD[d] for d in best) if len(dims) == 1: (d,) = dims score, summary = best[d][0], f"{best[d][0]} vectors in dimension {d} ({best[d][1]}); the record is {RECORD[d]}" else: score, summary = reached, "; ".join(f"d{d}: {best[d][0]}" for d in sorted(best)) + f"; {reached} of {len(dims)} dimensions at the record or above" pathlib.Path("metrics.json").write_text(json.dumps({"score": score, "score_exact": str(score), "subtasks": {f"d{d}": best[d][0] for d in sorted(best)}}) + "\n") print(summary) else: print(f"no valid candidates: {bad} failed" if bad else "no candidates to check") sys.exit(1 if bad else 0)