import json
from typing import Any, List
from arc_agi.types import ARCAGIResult
def _coerce_grid(x: Any) -> list:
try:
import numpy as _np
if isinstance(x, _np.ndarray):
return x.tolist()
except Exception:
pass
if isinstance(x, str):
s = x.strip()
if s and (s[0] == "[" or s[0] == "{"):
try:
parsed = json.loads(s)
return parsed
except Exception:
return []
else:
return []
if isinstance(x, list):
return x
return []
def build_kaggle_two_attempts(results: list[ARCAGIResult], test_in: List[List[List[int]]]):
"""
Returns: List[{"attempt_1": grid, "attempt_2": grid}] with len == len(test_in).
"""
num_tests = len(test_in)
out = []
for j in range(num_tests):
attempts: List[list] = []
for ar in results:
tr = ar.get("results", [])
if j < len(tr):
rr = tr[j]
grid = _coerce_grid(rr.get("output", []))
if grid != []:
attempts.append(grid)
if len(attempts) == 2:
break
while len(attempts) < 2:
attempts.append([])
out.append({"attempt_1": attempts[0], "attempt_2": attempts[1]})
return out