"""ALE-Bench ahc039 (Purse Seine Fishing): exact Python port of the official tools/src/lib.rs (parse_output + compute_score_details), vectorised with numpy (integer arithmetic only). Score = max(0, a - b + 1) for the LAST polygon output; tool errors -> Invalid.""" import re import numpy as np from run import Invalid D = 100000 MAX_CHARS = 2_000_000 _INT = re.compile(r"[+-]?[0-9]+\Z") def _tokens(ans): s = ans.get("output") if not isinstance(s, str): raise Invalid("output must be a string") if len(s) > MAX_CHARS: raise Invalid(f"output longer than {MAX_CHARS} characters") if any(not (" " <= ch <= "~" or ch in "\t\n\r") for ch in s): raise Invalid("output may contain printable ASCII, spaces, tabs and newlines only") return s.split() def _read(toks, k, lb, ub): if k >= len(toks): raise Invalid("Unexpected EOF") tok = toks[k] if not _INT.match(tok) or not (-2 ** 63 <= int(tok) < 2 ** 63): # Rust i64 / usize parse raise Invalid(f"Parse error: {tok}") v = int(tok) if v < lb or v > ub: raise Invalid(f"Out of range: {v}") return v def parse_input(text): t = list(map(int, text.split())) N = t[0] ps = np.array(t[1:1 + 4 * N], dtype=np.int64).reshape(2 * N, 2) return N, ps def check(inst, ans): toks = _tokens(ans) N, ps = parse_input(inst["input"]) polys, k = [], 0 while k < len(toks): tok = toks[k] if tok.startswith("-"): # m is read as usize: a sign '-' is a parse error raise Invalid(f"Parse error: {tok}") m = _read(toks, k, 4, 1000) k += 1 poly = [] for _ in range(m): poly.append((_read(toks, k, 0, D), _read(toks, k + 1, 0, D))) k += 2 polys.append(poly) out = polys[-1] if polys else [] m = len(out) if m == 0: return 1.0, {"mackerels": 0, "sardines": 0, "vertices": 0, "length": 0} length = 0 for i in range(m): p, q, r = out[i], out[(i + 1) % m], out[(i + 2) % m] if p == q: raise Invalid("Two consecutive vertices share the same coordinates.") elif p[0] == q[0]: length += abs(p[1] - q[1]) if q[0] == r[0] and (p[1] - q[1]) * (r[1] - q[1]) > 0: raise Invalid("The polygon is self-intersecting.") elif p[1] == q[1]: length += abs(p[0] - q[0]) if q[1] == r[1] and (p[0] - q[0]) * (r[0] - q[0]) > 0: raise Invalid("The polygon is self-intersecting.") else: raise Invalid(f"The {i}-th edge is not parallel to the axes.") P = np.array(out, dtype=np.int64) Q = np.roll(P, -1, axis=0) xlo, xhi = np.minimum(P[:, 0], Q[:, 0]), np.maximum(P[:, 0], Q[:, 0]) ylo, yhi = np.minimum(P[:, 1], Q[:, 1]), np.maximum(P[:, 1], Q[:, 1]) idx = np.arange(m) for off in range(2, m - 1): # non-adjacent edge pairs (i, i+off), as in the Rust loop j = (idx + off) % m hit = (np.maximum(xlo, xlo[j]) <= np.minimum(xhi, xhi[j])) & \ (np.maximum(ylo, ylo[j]) <= np.minimum(yhi, yhi[j])) if hit.any(): raise Invalid("The polygon is self-intersecting.") if length > 4 * D: raise Invalid(f"The length is too long: {length}.") rx, ry = ps[:, 0], ps[:, 1] on_edge = np.zeros(len(ps), dtype=bool) parity = np.zeros(len(ps), dtype=bool) for e in range(m): on_edge |= (xlo[e] <= rx) & (rx <= xhi[e]) & (ylo[e] <= ry) & (ry <= yhi[e]) if P[e, 1] == Q[e, 1]: parity ^= (P[e, 1] > ry) & (xlo[e] <= rx) & (rx < xhi[e]) inside = on_edge | parity a = int(inside[:N].sum()) b = int(inside[N:].sum()) return float(max(0, 1 + a - b)), {"mackerels": a, "sardines": b, "vertices": m, "length": length}