amphora's picture
Initial release: 3,577 construct/optimize math RL tasks with deterministic graders
1ca0407 verified
Raw History Blame Contribute Delete
1.08 kB
"""21 disjoint circles in a rectangle of perimeter 4, maximize the sum of radii
(EinsteinArena circles-rectangle, MIT).
Upstream bug fixed: the perimeter test is exact (Fractions) upstream but the disjointness test compares
float sqrt distances; both are exact here."""
from fractions import Fraction
from run import Invalid, float_matrix
def check(inst, ans):
n = inst["n"]
c = float_matrix(ans.get("circles"), n, 3, "circles")
F = [tuple(Fraction(v) for v in row) for row in c]
if any(r <= 0 for _, _, r in F):
raise Invalid("radii must be positive")
w = max(x + r for x, _, r in F) - min(x - r for x, _, r in F)
h = max(y + r for _, y, r in F) - min(y - r for _, y, r in F)
if w + h > 2:
raise Invalid("bounding rectangle violates width + height <= 2")
for i in range(n):
xi, yi, ri = F[i]
for j in range(i + 1, n):
xj, yj, rj = F[j]
if (xi - xj) ** 2 + (yi - yj) ** 2 < (ri + rj) ** 2:
raise Invalid(f"circles {i} and {j} overlap")
return sum(r for _, _, r in c), {}