"""ALE-Bench ahc032 (Mod Stamp): exact Python port of the official tools/src/lib.rs (parse_output + compute_score_details). Score = sum of (b_ij mod 998244353); WA -> Invalid. Only deviation: L = 0 (the do-nothing output) is rejected.""" import re from run import Invalid MOD = 998244353 MAX_CHARS = 100_000 _UINT = 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 _UINT.match(tok): 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, M, K = t[0], t[1], t[2] p = 3 a = [t[p + i * N: p + (i + 1) * N] for i in range(N)] p += N * N s = [] for _ in range(M): s.append([t[p + 3 * x: p + 3 * x + 3] for x in range(3)]) p += 9 return N, M, K, a, s def check(inst, ans): toks = _tokens(ans) N, M, K, a, s = parse_input(inst["input"]) L = _read(toks, 0, 0, K) if L == 0: # conversion deviation (stricter than the tool): the empty plan L = 0 is not accepted raise Invalid("this conversion requires at least one operation (L >= 1)") ops, k = [], 1 for _ in range(L): m = _read(toks, k, 0, M - 1) i = _read(toks, k + 1, 0, N - 3) j = _read(toks, k + 2, 0, N - 3) ops.append((m, i, j)) k += 3 if k < len(toks): raise Invalid("Too many outputs") b = [list(r) for r in a] for m, i, j in ops: for x in range(3): for y in range(3): b[i + x][j + y] = (b[i + x][j + y] + s[m][x][y]) % MOD score = sum(map(sum, b)) return float(score), {"operations": L}