amphora's picture
v2: add 216 optimize tasks / 77 families (Tao constants, AlphaEvolve, Leaps in Bounds, FrontierShift, FrontierCO MDS, ALE-Bench, FrontierCS, EdgeBench)
ac7c4a9 verified
Raw History Blame Contribute Delete
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}