Download graders/ale_ahc009/check.py from amphora/MathConstructOptimize-Envs: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ale_ahc009/check.py
- Command line
-
hf download hf://datasets/amphora/MathConstructOptimize-Envs/graders/ale_ahc009/check.py
-
curl -L -o check.py https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ale_ahc009/check.py
2.48 kB
| """ALE-Bench ahc009 (AHC 009 "Robust Memory of Commuting Routes"): Python port of the official | |
| tools/src/lib.rs (parse_output + compute_score), replicating the f64 operation order exactly. | |
| Answer = the contestant's stdout text (a U/D/L/R string of length <= 200).""" | |
| import math | |
| from run import Invalid | |
| N = 20 | |
| L = 200 | |
| DIJ = [(-1, 0), (0, -1), (1, 0), (0, 1)] | |
| DIR = "ULDR" | |
| _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 rust_round(x): # f64::round, half away from zero | |
| r = math.floor(abs(x)) | |
| r = r + 1 if abs(x) - r >= 0.5 else r | |
| return r if x >= 0 else -r | |
| def check(inst, ans): | |
| out = ans.get("output") | |
| if not isinstance(out, str): | |
| raise Invalid("output must be a string") | |
| if len(out) > 10_000: | |
| raise Invalid("too long output") | |
| if not out.isascii(): | |
| raise Invalid("output must be ASCII") | |
| tok = inst["input"].split() | |
| si, sj, ti, tj = (int(x) for x in tok[:4]) | |
| p = float(tok[4]) | |
| hs = [[c == "1" for c in tok[5 + i]] for i in range(N)] | |
| vs = [[c == "1" for c in tok[5 + N + i]] for i in range(N - 1)] | |
| def can_move(i, j, d): | |
| if d == 0: | |
| return i > 0 and not vs[i - 1][j] | |
| if d == 1: | |
| return j > 0 and not hs[i][j - 1] | |
| if d == 2: | |
| return i < N - 1 and not vs[i][j] | |
| return j < N - 1 and not hs[i][j] | |
| out = rust_trim(out) | |
| crt = [[0.0] * N for _ in range(N)] | |
| crt[si][sj] = 1.0 | |
| s = 0.0 | |
| goal = 0.0 | |
| q = 1.0 - p | |
| for t, ch in enumerate(out): | |
| if t >= L: | |
| raise Invalid("too long output") | |
| d = DIR.find(ch) | |
| if d < 0: | |
| raise Invalid(f"illegal char: {ch}") | |
| nxt = [[0.0] * N for _ in range(N)] | |
| di, dj = DIJ[d] | |
| for i in range(N): | |
| row = crt[i] | |
| for j in range(N): | |
| c = row[j] | |
| if c > 0.0: | |
| if can_move(i, j, d): | |
| nxt[i + di][j + dj] += c * q | |
| nxt[i][j] += c * p | |
| else: | |
| nxt[i][j] += c | |
| crt = nxt | |
| s += crt[ti][tj] * (2 * L - t) | |
| goal += crt[ti][tj] | |
| crt[ti][tj] = 0.0 | |
| return float(rust_round(1e8 * s / (2 * L))), {"length": len(out), "p_reach": goal} | |