"""ALE-Bench ahc004 (AHC 004 "Alien's Genome Assembly"): Python port of the official tools/src/lib.rs (parse_output + compute_score_detail). Answer = the contestant's stdout text (N lines of length N).""" import math import re from run import Invalid N = 20 MAX_LEN = 100_000 # Rust split_whitespace() splits on Unicode White_Space (Python's str.split also splits on \x1c-\x1f) _WS_RE = re.compile("[\t\n\x0b\x0c\r \x85\xa0  - 

   ]+") def rust_split_ws(s): return [t for t in _WS_RE.split(s) if t] def rust_round(x): # f64::round (half away from zero), x >= 0 r = math.floor(x) return r + 1 if x - r >= 0.5 else r def check(inst, ans): out = ans.get("output") if not isinstance(out, str): raise Invalid("output must be a string") if len(out) > MAX_LEN: raise Invalid(f"output longer than {MAX_LEN} characters") if not out.isascii(): raise Invalid("output must be ASCII") tok = inst["input"].split() M = int(tok[1]) S = tok[2:2 + M] rows = rust_split_ws(out) if len(rows) < N: raise Invalid("Unexpected EOF") if len(rows) > N: raise Invalid("Too many lines") d = 0 for r in rows: if len(r) != N: raise Invalid(f"illegal length: {len(r)}") for ch in r: if not ("A" <= ch <= "H") and ch != ".": raise Invalid(f"illegal char: {ch}") if ch == ".": d += 1 present = set() for i in range(N): for j in range(N): for di, dj in ((0, 1), (1, 0)): s = [] for k in range(12): s.append(rows[(i + di * k) % N][(j + dj * k) % N]) present.add("".join(s)) c = sum(1 for s in S if s in present) if c < M: score = rust_round(1e8 * c / M) else: score = rust_round(1e8 * (2 * N * N) / (2 * N * N - d)) return float(score), {"c": c, "M": M, "dots": d}