Download graders/ale_ahc024/check.py from amphora/MathConstructOptimize-Envs: direct link, hf CLI and curl.
- Browser
- Download file 3.44 kB
-
https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ale_ahc024/check.py
- Command line
-
hf download hf://datasets/amphora/MathConstructOptimize-Envs/graders/ale_ahc024/check.py
-
curl -L -o check.py https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ale_ahc024/check.py
3.44 kB
| """ALE-Bench ahc024 (Topological Map): exact Python port of the official tools/src/lib.rs | |
| (parse_output + compute_score). Score = 1 + #squares of colour 0 in the LAST map output; WA -> Invalid.""" | |
| import re | |
| from run import Invalid | |
| MAX_CHARS = 2_000_000 | |
| _UINT = re.compile(r"\+?[0-9]+\Z") | |
| def _text(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 | |
| def _read(tok, lb, ub): | |
| if tok is None: | |
| raise Invalid("Unexpected EOF") | |
| 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 = text.split() | |
| n, m = int(t[0]), int(t[1]) | |
| cs = [[int(t[2 + i * n + j]) for j in range(n)] for i in range(n)] | |
| return n, m, cs | |
| def _adj(cs, n, m): | |
| adj = set() | |
| for i in range(n): | |
| for j in range(n): | |
| c = cs[i][j] | |
| if c != 0 and (i == 0 or j == 0 or i + 1 == n or j + 1 == n): | |
| adj.add((0, c)) | |
| if i + 1 < n and c != cs[i + 1][j]: | |
| d = cs[i + 1][j] | |
| adj.add((min(c, d), max(c, d))) | |
| if j + 1 < n and c != cs[i][j + 1]: | |
| d = cs[i][j + 1] | |
| adj.add((min(c, d), max(c, d))) | |
| return adj | |
| def check(inst, ans): | |
| s = _text(ans) | |
| n, m, cs = parse_input(inst["input"]) | |
| outs, out = [], [] | |
| for line in s.split("\n"): | |
| line = line.strip() | |
| if not line: | |
| continue | |
| toks = line.split() | |
| row = [_read(toks[k] if k < len(toks) else None, 0, m) for k in range(n)] | |
| if len(toks) > n: | |
| raise Invalid("illegal output format") | |
| out.append(row) | |
| if len(out) == n: | |
| outs.append(out) | |
| out = [] | |
| if out: | |
| raise Invalid("illegal output format") | |
| if not outs: | |
| raise Invalid("empty output") | |
| d = outs[-1] | |
| a0, a1 = _adj(cs, n, m), _adj(d, n, m) | |
| if a0 != a1: | |
| raise Invalid("adjacency differs from the original map") | |
| done = [False] * (m + 1) | |
| vis = [[False] * n for _ in range(n)] | |
| for i in range(n): | |
| for j in range(n): | |
| if vis[i][j]: | |
| continue | |
| c = d[i][j] | |
| if c > 0: | |
| if done[c]: | |
| raise Invalid(f"Squares of color {c} are not connected.") | |
| done[c] = True | |
| stack = [(i, j)] | |
| vis[i][j] = True | |
| outer = False | |
| while stack: | |
| x, y = stack.pop() | |
| for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)): | |
| x2, y2 = x + dx, y + dy | |
| if not (0 <= x2 < n and 0 <= y2 < n): | |
| outer = True | |
| elif d[x][y] == d[x2][y2] and not vis[x2][y2]: | |
| vis[x2][y2] = True | |
| stack.append((x2, y2)) | |
| if c == 0 and not outer: | |
| raise Invalid("Squares of color 0 are not connected.") | |
| zeros = sum(row.count(0) for row in d) | |
| return float(1 + zeros), {"zeros": zeros, "maps_output": len(outs)} | |