Download graders/ale_ahc014/check.py from amphora/MathConstructOptimize-Envs: direct link, hf CLI and curl.
- Browser
- Download file 4.47 kB
-
https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ale_ahc014/check.py
- Command line
-
hf download hf://datasets/amphora/MathConstructOptimize-Envs/graders/ale_ahc014/check.py
-
curl -L -o check.py https://huggingface.co/datasets/amphora/MathConstructOptimize-Envs/resolve/main/graders/ale_ahc014/check.py
4.47 kB
| """ALE-Bench ahc014 (AHC 014 "RectJoin"): Python port of the official tools/src/lib.rs (parse_output, | |
| State::check_move / apply_move, compute_score). Answer = the contestant's stdout text.""" | |
| import math | |
| import re | |
| from run import Invalid | |
| DXY = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)] | |
| DIRI = {v: k for k, v in enumerate(DXY)} | |
| _INT = re.compile(r"[+-]?[0-9]+\Z") | |
| _WS = re.compile(r"[\t\n\x0b\x0c\r ]+") # output must be ASCII; Rust split_whitespace on ASCII = these | |
| MAX_LEN = 2_000_000 | |
| def rust_round(x): | |
| r = math.floor(x) | |
| return r + 1 if x - r >= 0.5 else r | |
| def read(tokens, pos, lb, ub): | |
| if pos >= len(tokens): | |
| raise Invalid("Unexpected EOF") | |
| t = tokens[pos] | |
| if not _INT.match(t): | |
| raise Invalid(f"Parse error: {t[:20]}") | |
| v = int(t) | |
| if not (-2 ** 31 <= v < 2 ** 31): | |
| raise Invalid(f"Parse error: {t[:20]}") | |
| if v < lb or ub < v: | |
| raise Invalid(f"Out of range: {v}") | |
| return v | |
| def weight(p, N): | |
| dx = p[0] - N // 2 | |
| dy = p[1] - N // 2 | |
| return dx * dx + dy * dy + 1 | |
| def sgn(v): | |
| return (v > 0) - (v < 0) | |
| 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") | |
| tok = inst["input"].split() | |
| N, M = int(tok[0]), int(tok[1]) | |
| ps = [(int(tok[2 + 2 * i]), int(tok[3 + 2 * i])) for i in range(M)] | |
| tokens = [t for t in _WS.split(out) if t] | |
| K = read(tokens, 0, 0, 1000000000) | |
| if K == 0: | |
| raise Invalid("K = 0 is not accepted in this conversion (perform at least one operation)") | |
| pos = 1 | |
| rects = [] | |
| for _ in range(K): | |
| r = [] | |
| for i in range(4): | |
| x = read(tokens, pos, 0, N - 1) | |
| y = read(tokens, pos + 1, 0, N - 1) | |
| pos += 2 | |
| r.append((x, y)) | |
| rects.append(r) | |
| has = [[False] * N for _ in range(N)] | |
| used = [[[False] * 8 for _ in range(N)] for _ in range(N)] | |
| for (x, y) in ps: | |
| has[x][y] = True | |
| for t, rect in enumerate(rects): | |
| err = None | |
| bad = [i for i in (1, 2, 3) if not has[rect[i][0]][rect[i][1]]] | |
| if bad: | |
| err = f"{rect[bad[0]]} does not contain a dot" | |
| elif has[rect[0][0]][rect[0][1]]: | |
| err = f"{rect[0]} already contains a dot" | |
| else: | |
| dx01 = rect[1][0] - rect[0][0] | |
| dy01 = rect[1][1] - rect[0][1] | |
| dx03 = rect[3][0] - rect[0][0] | |
| dy03 = rect[3][1] - rect[0][1] | |
| if dx01 * dx03 + dy01 * dy03 != 0: | |
| err = "Illegal rectangle" | |
| elif dx01 != 0 and dy01 != 0 and abs(dx01) != abs(dy01): | |
| err = "Illegal rectangle" | |
| elif (rect[1][0] + dx03, rect[1][1] + dy03) != rect[2]: | |
| err = "Illegal rectangle" | |
| else: | |
| for i in range(4): | |
| x, y = rect[i] | |
| tx, ty = rect[(i + 1) % 4] | |
| dx, dy = sgn(tx - x), sgn(ty - y) | |
| d = DIRI[(dx, dy)] | |
| while (x, y) != (tx, ty): | |
| if (x, y) != rect[i] and has[x][y]: | |
| err = f"There is an obstacle at ({x}, {y})" | |
| break | |
| if used[x][y][d]: | |
| err = "Overlapped rectangles" | |
| break | |
| x += dx | |
| y += dy | |
| if used[x][y][d ^ 4]: | |
| err = "Overlapped rectangles" | |
| break | |
| if err: | |
| break | |
| if err: | |
| raise Invalid(f"{err} (turn: {t})") | |
| has[rect[0][0]][rect[0][1]] = True | |
| for i in range(4): | |
| x, y = rect[i] | |
| tx, ty = rect[(i + 1) % 4] | |
| dx, dy = sgn(tx - x), sgn(ty - y) | |
| d = DIRI[(dx, dy)] | |
| while (x, y) != (tx, ty): | |
| used[x][y][d] = True | |
| x += dx | |
| y += dy | |
| used[x][y][d ^ 4] = True | |
| num = sum(weight(p, N) for p in ps) + sum(weight(r[0], N) for r in rects) | |
| den = sum(weight((i, j), N) for i in range(N) for j in range(N)) | |
| score = rust_round(1e6 * (N * N) / M * num / den) | |
| return float(score), {"K": K} | |