Download graders/ale_ahc010/check.py from amphora/MathConstructOptimize-Envs: direct link, hf CLI and curl.
- Browser
- Download file 3.02 kB
-
https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ale_ahc010/check.py
- Command line
-
hf download hf://datasets/amphora/MathConstructOptimize-Envs/graders/ale_ahc010/check.py
-
curl -L -o check.py https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ale_ahc010/check.py
3.02 kB
| """ALE-Bench ahc010 (AHC 010 "Loop Lines"): Python port of the official tools/src/lib.rs (parse_output + | |
| compute_score; the tester scores the LAST output line). Answer = the contestant's stdout text.""" | |
| from run import Invalid | |
| N = 30 | |
| MAX_LEN = 200_000 | |
| ROTATE = [1, 2, 3, 0, 5, 4, 7, 6] | |
| X = -1 | |
| TO = [[1, 0, X, X], [3, X, X, 0], [X, X, 3, 2], [X, 2, 1, X], [1, 0, 3, 2], [3, 2, 1, 0], [2, X, 0, X], [X, 3, X, 1]] | |
| DIJ = [(0, -1), (-1, 0), (0, 1), (1, 0)] | |
| _WS = set("\t\n\x0b\x0c\r ") # output is required to be ASCII, so Rust's trim() reduces to these | |
| def rust_lines(s): | |
| parts = s.split("\n") | |
| if parts and parts[-1] == "": | |
| parts.pop() | |
| return [p[:-1] if p.endswith("\r") else p for p in parts] | |
| 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 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") | |
| rows = inst["input"].split() | |
| tiles0 = [[ord(c) - 48 for c in rows[i]] for i in range(N)] | |
| outs = [] | |
| for line in rust_lines(out): | |
| line = rust_trim(line) | |
| tmp = [] | |
| for c in line: | |
| if c < "0" or "3" < c: | |
| raise Invalid(f"Illegal output: {c}") | |
| tmp.append(ord(c) - 48) | |
| if len(tmp) != N * N: | |
| raise Invalid(f"Illegal output length: {len(line)}") | |
| outs.append(tmp) | |
| if not outs: | |
| raise Invalid("empty output") | |
| r = outs[-1] | |
| tiles = [row[:] for row in tiles0] | |
| for i in range(N): | |
| for j in range(N): | |
| for _ in range(r[i * N + j]): | |
| tiles[i][j] = ROTATE[tiles[i][j]] | |
| used = [[[False] * 4 for _ in range(N)] for _ in range(N)] | |
| ls = [] | |
| for i in range(N): | |
| for j in range(N): | |
| for d in range(4): | |
| if TO[tiles[i][j]][d] != X and not used[i][j][d]: | |
| i2, j2, d2 = i, j, d | |
| length = 0 | |
| while not used[i2][j2][d2]: | |
| if TO[tiles[i2][j2]][d2] == X: | |
| break | |
| length += 1 | |
| used[i2][j2][d2] = True | |
| d2 = TO[tiles[i2][j2]][d2] | |
| used[i2][j2][d2] = True | |
| i2 += DIJ[d2][0] | |
| j2 += DIJ[d2][1] | |
| if not (0 <= i2 < N and 0 <= j2 < N): | |
| break | |
| d2 = (d2 + 2) % 4 | |
| if (i, j, d) == (i2, j2, d2): | |
| ls.append(length) | |
| if len(ls) <= 1: | |
| score = 0 | |
| else: | |
| ls.sort() | |
| score = ls[-1] * ls[-2] | |
| return float(score), {"loops": len(ls), "L1": ls[-1] if ls else 0, "L2": ls[-2] if len(ls) > 1 else 0} | |