"""ALE-Bench ahc002 (AtCoder Heuristic Contest 002 "Walking on Tiles"): Python port of the official tools/src/lib.rs (read_input / read_output / compute_score_detail). Answer = the contestant's stdout text. Score = sum of p over visited squares (incl. start); any rule violation = WA -> Invalid.""" from run import Invalid N = 50 MAX_LEN = 100_000 # Rust str::trim() strips Unicode White_Space (Python's str.strip also strips \x1c-\x1f, so do it by hand) _WS = set("\t\n\x0b\x0c\r \x85\xa0            " "

   ") def rust_trim(s): a, b = 0, len(s) while a < b and s[a] in _WS: a += 1 while b > a and s[b - 1] in _WS: b -= 1 return s[a:b] def parse_input(text): t = [int(x) for x in text.split()] si, sj = t[0], t[1] tiles = [t[2 + i * N: 2 + (i + 1) * N] for i in range(N)] o = 2 + N * N ps = [t[o + i * N: o + (i + 1) * N] for i in range(N)] return (si, sj), tiles, ps 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") (i, j), tiles, ps = parse_input(inst["input"]) out = rust_trim(out) used = set([tiles[i][j]]) score = ps[i][j] D = {"L": (0, -1), "R": (0, 1), "U": (-1, 0), "D": (1, 0)} for c in out: if c not in D: raise Invalid("Illegal output") di, dj = D[c] i += di j += dj if not (0 <= i < N and 0 <= j < N): raise Invalid("Out of range") tl = tiles[i][j] if tl in used: raise Invalid("Stepped on the same tile twice") used.add(tl) score += ps[i][j] return float(score), {"moves": len(out)}