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.44 kB
"""ALE-Bench ahc021 (AHC 021 "Pyramid Sorting"): Python port of the official tools/src/lib.rs
(parse_output + compute_score + is_adj). Answer = the contestant's stdout text."""
import re
from run import Invalid
N = 30
_UINT = re.compile(r"\+?[0-9]+\Z") # Rust usize::from_str: optional '+', ASCII digits
_WS = re.compile(r"[\t\n\x0b\x0c\r ]+")
MAX_LEN = 1_000_000
def read(tokens, pos, lb, ub):
if pos >= len(tokens):
raise Invalid("Unexpected EOF")
t = tokens[pos]
if not _UINT.match(t) or len(t) > 25 or int(t) >= 2 ** 64:
raise Invalid(f"Parse error: {t[:20]}")
v = int(t)
if v < lb or ub < v:
raise Invalid(f"Out of range: {v}")
return v
def is_adj(p, q):
(x1, y1), (x2, y2) = p, q
if x1 == x2:
return y1 == y2 + 1 or y1 + 1 == y2
if x1 + 1 == x2:
return y1 == y2 or y1 + 1 == y2
if x1 == x2 + 1:
return y1 == y2 or y1 == y2 + 1
return False
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")
vals = [int(t) for t in inst["input"].split()]
bs, k = [], 0
for i in range(N):
bs.append(vals[k:k + i + 1])
k += i + 1
tokens = [t for t in _WS.split(out) if t]
K = read(tokens, 0, 0, 10000)
if K == 0:
raise Invalid("K = 0 is not accepted in this conversion (perform at least one operation)")
p = 1
ops = []
for _ in range(K):
x1 = read(tokens, p, 0, N - 1)
y1 = read(tokens, p + 1, 0, x1)
x2 = read(tokens, p + 2, 0, N - 1)
y2 = read(tokens, p + 3, 0, x2)
p += 4
ops.append(((x1, y1), (x2, y2)))
if len(tokens) > p:
raise Invalid("Too many output")
for t, (a, b) in enumerate(ops):
if not is_adj(a, b):
raise Invalid(f"{a} and {b} are not adjacent (turn {t})")
bs[a[0]][a[1]], bs[b[0]][b[1]] = bs[b[0]][b[1]], bs[a[0]][a[1]]
num = 0
for x in range(N - 1):
for y in range(x + 1):
if bs[x][y] > bs[x + 1][y]:
num += 1
if bs[x][y] > bs[x + 1][y + 1]:
num += 1
score = (10000 - K) * 5 + 50000 if num == 0 else 50000 - num * 50
return float(score), {"K": K, "E": num}