broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def attacked(bd, sq, by_white):
        f,r=ord(sq[0])-97,int(sq[1])-1
        def at(ff,rr):
            return bd.get(chr(97+ff)+str(rr+1)) if 0<=ff<8 and 0<=rr<8 else None
        P,N,B,R,Q,K=('PNBRQK' if by_white else 'pnbrqk')
        pr=r-1 if by_white else r+1
        if at(f-1,pr)==P or at(f+1,pr)==P: return True
        for df,dr in ((1,2),(2,1),(-1,2),(-2,1),(1,-2),(2,-1),(-1,-2),(-2,-1)):
            if at(f+df,r+dr)==N: return True
        for df in (-1,0,1):
            for dr in (-1,0,1):
                if (df or dr) and at(f+df,r+dr)==K: return True
        for df,dr,kinds in ((1,0,(R,Q)),(-1,0,(R,Q)),(0,1,(R,Q)),(0,-1,(R,Q)),(1,1,(B,Q)),(1,-1,(B,Q)),(-1,1,(B,Q)),(-1,-1,(B,Q))):
            ff,rr=f+df,r+dr
            while 0<=ff<8 and 0<=rr<8:
                v=at(ff,rr)
                if v:
                    if v in kinds: return True
                    break
                ff+=df; rr+=dr
        return False

    b=x['board']; white=x['side']=='w'; rank='1' if white else '8'
    kf=ord(x['king_from'][0])-97; rf=ord(x['rook_from'][0])-97
    if b.get(x['king_from'])!=('K' if white else 'k') or b.get(x['rook_from'])!=('R' if white else 'r'): return False
    if x['wing']=='K':
        if rf<kf: return False
        kd,rd=6,5
    else:
        if rf>kf: return False
        kd,rd=2,3
    lo=min(kf,kd,rf,rd); hi=max(kf,kd,rf,rd)
    for f in range(lo,hi+1):
        q=chr(97+f)+rank
        if q in b and f!=kf: return False
    step=1 if kd>=kf else -1
    for f in range(kf,kd+step,step):
        if attacked(b, chr(97+f)+rank, not white): return False
    return True
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True)],
 [('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True)]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def attacked(bd, sq, by_white):
        f,r=ord(sq[0])-97,int(sq[1])-1
        def at(ff,rr):
            return bd.get(chr(97+ff)+str(rr+1)) if 0<=ff<8 and 0<=rr<8 else None
        P,N,B,R,Q,K=('PNBRQK' if by_white else 'pnbrqk')
        pr=r-1 if by_white else r+1
        if at(f-1,pr)==P or at(f+1,pr)==P: return True
        for df,dr in ((1,2),(2,1),(-1,2),(-2,1),(1,-2),(2,-1),(-1,-2),(-2,-1)):
            if at(f+df,r+dr)==N: return True
        for df in (-1,0,1):
            for dr in (-1,0,1):
                if (df or dr) and at(f+df,r+dr)==K: return True
        for df,dr,kinds in ((1,0,(R,Q)),(-1,0,(R,Q)),(0,1,(R,Q)),(0,-1,(R,Q)),(1,1,(B,Q)),(1,-1,(B,Q)),(-1,1,(B,Q)),(-1,-1,(B,Q))):
            ff,rr=f+df,r+dr
            while 0<=ff<8 and 0<=rr<8:
                v=at(ff,rr)
                if v:
                    if v in kinds: return True
                    break
                ff+=df; rr+=dr
        return False

    b=x['board']; white=x['side']=='w'; rank='1' if white else '8'
    kf=ord(x['king_from'][0])-97; rf=ord(x['rook_from'][0])-97
    if b.get(x['king_from'])!=('K' if white else 'k') or b.get(x['rook_from'])!=('R' if white else 'r'): return False
    if x['wing']=='K':
        if rf<kf: return False
        kd,rd=6,5
    else:
        if rf>kf: return False
        kd,rd=2,3
    lo=min(kf,kd,rf,rd); hi=max(kf,kd,rf,rd)
    for f in range(lo,hi+1):
        q=chr(97+f)+rank
        if q in b and f!=rf: return False
    step=1 if kd>=kf else -1
    for f in range(kf,kd+step,step):
        if attacked(b, chr(97+f)+rank, not white): return False
    return True
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True)],
 [('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True)]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
King b1 with rook c1 cannot castle kingside because the king must pass the rook's square.

Input: {"board", "side", "wing", "king_from", "rook_from"}; rights are assumed held. Chess960 castling: the king ends on g (kingside) or c (queenside) and the rook on f or d; the kingside rook must be on the king's h-side and the queenside rook on its a-side. Every square from the leftmost to the rightmost of king start, king end, rook start and rook end must be empty apart from the castling king and rook, and no square the king stands on or passes through (start to end inclusive) may be attacked.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def attacked(bd, sq, by_white):
        f,r=ord(sq[0])-97,int(sq[1])-1
        def at(ff,rr):
            return bd.get(chr(97+ff)+str(rr+1)) if 0<=ff<8 and 0<=rr<8 else None
        P,N,B,R,Q,K=('PNBRQK' if by_white else 'pnbrqk')
        pr=r-1 if by_white else r+1
        if at(f-1,pr)==P or at(f+1,pr)==P: return True
        for df,dr in ((1,2),(2,1),(-1,2),(-2,1),(1,-2),(2,-1),(-1,-2),(-2,-1)):
            if at(f+df,r+dr)==N: return True
        for df in (-1,0,1):
            for dr in (-1,0,1):
                if (df or dr) and at(f+df,r+dr)==K: return True
        for df,dr,kinds in ((1,0,(R,Q)),(-1,0,(R,Q)),(0,1,(R,Q)),(0,-1,(R,Q)),(1,1,(B,Q)),(1,-1,(B,Q)),(-1,1,(B,Q)),(-1,-1,(B,Q))):
            ff,rr=f+df,r+dr
            while 0<=ff<8 and 0<=rr<8:
                v=at(ff,rr)
                if v:
                    if v in kinds: return True
                    break
                ff+=df; rr+=dr
        return False

    b=x['board']; white=x['side']=='w'; rank='1' if white else '8'
    kf=ord(x['king_from'][0])-97; rf=ord(x['rook_from'][0])-97
    if b.get(x['king_from'])!=('K' if white else 'k') or b.get(x['rook_from'])!=('R' if white else 'r'): return False
    if x['wing']=='K':
        if rf<kf: return False
        kd,rd=6,5
    else:
        if rf>kf: return False
        kd,rd=2,3
    lo=min(kf,kd); hi=max(kf,kd)
    for f in range(lo,hi+1):
        q=chr(97+f)+rank
        if q in b and f not in (kf,rf): return False
    step=1 if kd>=kf else -1
    for f in range(kf,kd+step,step):
        if attacked(b, chr(97+f)+rank, not white): return False
    return True
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def attacked(bd, sq, by_white):
        f,r=ord(sq[0])-97,int(sq[1])-1
        def at(ff,rr):
            return bd.get(chr(97+ff)+str(rr+1)) if 0<=ff<8 and 0<=rr<8 else None
        P,N,B,R,Q,K=('PNBRQK' if by_white else 'pnbrqk')
        pr=r-1 if by_white else r+1
        if at(f-1,pr)==P or at(f+1,pr)==P: return True
        for df,dr in ((1,2),(2,1),(-1,2),(-2,1),(1,-2),(2,-1),(-1,-2),(-2,-1)):
            if at(f+df,r+dr)==N: return True
        for df in (-1,0,1):
            for dr in (-1,0,1):
                if (df or dr) and at(f+df,r+dr)==K: return True
        for df,dr,kinds in ((1,0,(R,Q)),(-1,0,(R,Q)),(0,1,(R,Q)),(0,-1,(R,Q)),(1,1,(B,Q)),(1,-1,(B,Q)),(-1,1,(B,Q)),(-1,-1,(B,Q))):
            ff,rr=f+df,r+dr
            while 0<=ff<8 and 0<=rr<8:
                v=at(ff,rr)
                if v:
                    if v in kinds: return True
                    break
                ff+=df; rr+=dr
        return False

    b=x['board']; white=x['side']=='w'; rank='1' if white else '8'
    kf=ord(x['king_from'][0])-97; rf=ord(x['rook_from'][0])-97
    if b.get(x['king_from'])!=('K' if white else 'k') or b.get(x['rook_from'])!=('R' if white else 'r'): return False
    if x['wing']=='K':
        if rf<kf: return False
        kd,rd=6,5
    else:
        if rf>kf: return False
        kd,rd=2,3
    lo=min(kf,kd,rf); hi=max(kf,kd,rf)
    for f in range(lo,hi+1):
        q=chr(97+f)+rank
        if q in b and f not in (kf,rf): return False
    step=1 if kd>=kf else -1
    for f in range(kf,kd+step,step):
        if attacked(b, chr(97+f)+rank, not white): return False
    return True
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
King g1, rook h1 castles kingside with a bishop on f1.

Input: {"board", "side", "wing", "king_from", "rook_from"}; rights are assumed held. Chess960 castling: the king ends on g (kingside) or c (queenside) and the rook on f or d; the kingside rook must be on the king's h-side and the queenside rook on its a-side. Every square from the leftmost to the rightmost of king start, king end, rook start and rook end must be empty apart from the castling king and rook, and no square the king stands on or passes through (start to end inclusive) may be attacked.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def attacked(bd, sq, by_white):
        f,r=ord(sq[0])-97,int(sq[1])-1
        def at(ff,rr):
            return bd.get(chr(97+ff)+str(rr+1)) if 0<=ff<8 and 0<=rr<8 else None
        P,N,B,R,Q,K=('PNBRQK' if by_white else 'pnbrqk')
        pr=r-1 if by_white else r+1
        if at(f-1,pr)==P or at(f+1,pr)==P: return True
        for df,dr in ((1,2),(2,1),(-1,2),(-2,1),(1,-2),(2,-1),(-1,-2),(-2,-1)):
            if at(f+df,r+dr)==N: return True
        for df in (-1,0,1):
            for dr in (-1,0,1):
                if (df or dr) and at(f+df,r+dr)==K: return True
        for df,dr,kinds in ((1,0,(R,Q)),(-1,0,(R,Q)),(0,1,(R,Q)),(0,-1,(R,Q)),(1,1,(B,Q)),(1,-1,(B,Q)),(-1,1,(B,Q)),(-1,-1,(B,Q))):
            ff,rr=f+df,r+dr
            while 0<=ff<8 and 0<=rr<8:
                v=at(ff,rr)
                if v:
                    if v in kinds: return True
                    break
                ff+=df; rr+=dr
        return False

    b=x['board']; white=x['side']=='w'; rank='1' if white else '8'
    kf=ord(x['king_from'][0])-97; rf=ord(x['rook_from'][0])-97
    if b.get(x['king_from'])!=('K' if white else 'k') or b.get(x['rook_from'])!=('R' if white else 'r'): return False
    if x['wing']=='K':
        if rf<kf: return False
        kd,rd=6,5
    else:
        if rf>kf: return False
        kd,rd=2,3
    lo=min(kf,kd,rf,rd); hi=max(kf,kd,rf,rd)
    for f in range(lo,hi+1):
        q=chr(97+f)+rank
        if q in b and f not in (kf,rf): return False
    step=1 if kd>=kf else -1
    for f in range(kf,kd,step):
        if attacked(b, chr(97+f)+rank, not white): return False
    return True
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def attacked(bd, sq, by_white):
        f,r=ord(sq[0])-97,int(sq[1])-1
        def at(ff,rr):
            return bd.get(chr(97+ff)+str(rr+1)) if 0<=ff<8 and 0<=rr<8 else None
        P,N,B,R,Q,K=('PNBRQK' if by_white else 'pnbrqk')
        pr=r-1 if by_white else r+1
        if at(f-1,pr)==P or at(f+1,pr)==P: return True
        for df,dr in ((1,2),(2,1),(-1,2),(-2,1),(1,-2),(2,-1),(-1,-2),(-2,-1)):
            if at(f+df,r+dr)==N: return True
        for df in (-1,0,1):
            for dr in (-1,0,1):
                if (df or dr) and at(f+df,r+dr)==K: return True
        for df,dr,kinds in ((1,0,(R,Q)),(-1,0,(R,Q)),(0,1,(R,Q)),(0,-1,(R,Q)),(1,1,(B,Q)),(1,-1,(B,Q)),(-1,1,(B,Q)),(-1,-1,(B,Q))):
            ff,rr=f+df,r+dr
            while 0<=ff<8 and 0<=rr<8:
                v=at(ff,rr)
                if v:
                    if v in kinds: return True
                    break
                ff+=df; rr+=dr
        return False

    b=x['board']; white=x['side']=='w'; rank='1' if white else '8'
    kf=ord(x['king_from'][0])-97; rf=ord(x['rook_from'][0])-97
    if b.get(x['king_from'])!=('K' if white else 'k') or b.get(x['rook_from'])!=('R' if white else 'r'): return False
    if x['wing']=='K':
        if rf<kf: return False
        kd,rd=6,5
    else:
        if rf>kf: return False
        kd,rd=2,3
    lo=min(kf,kd,rf,rd); hi=max(kf,kd,rf,rd)
    for f in range(lo,hi+1):
        q=chr(97+f)+rank
        if q in b and f not in (kf,rf): return False
    step=1 if kd>=kf else -1
    for f in range(kf+step,kd+step,step):
        if attacked(b, chr(97+f)+rank, not white): return False
    return True
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Castling to g1 is allowed while a rook on g8 attacks it.

Input: {"board", "side", "wing", "king_from", "rook_from"}; rights are assumed held. Chess960 castling: the king ends on g (kingside) or c (queenside) and the rook on f or d; the kingside rook must be on the king's h-side and the queenside rook on its a-side. Every square from the leftmost to the rightmost of king start, king end, rook start and rook end must be empty apart from the castling king and rook, and no square the king stands on or passes through (start to end inclusive) may be attacked.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def attacked(bd, sq, by_white):
        f,r=ord(sq[0])-97,int(sq[1])-1
        def at(ff,rr):
            return bd.get(chr(97+ff)+str(rr+1)) if 0<=ff<8 and 0<=rr<8 else None
        P,N,B,R,Q,K=('PNBRQK' if by_white else 'pnbrqk')
        pr=r-1 if by_white else r+1
        if at(f-1,pr)==P or at(f+1,pr)==P: return True
        for df,dr in ((1,2),(2,1),(-1,2),(-2,1),(1,-2),(2,-1),(-1,-2),(-2,-1)):
            if at(f+df,r+dr)==N: return True
        for df in (-1,0,1):
            for dr in (-1,0,1):
                if (df or dr) and at(f+df,r+dr)==K: return True
        for df,dr,kinds in ((1,0,(R,Q)),(-1,0,(R,Q)),(0,1,(R,Q)),(0,-1,(R,Q)),(1,1,(B,Q)),(1,-1,(B,Q)),(-1,1,(B,Q)),(-1,-1,(B,Q))):
            ff,rr=f+df,r+dr
            while 0<=ff<8 and 0<=rr<8:
                v=at(ff,rr)
                if v:
                    if v in kinds: return True
                    break
                ff+=df; rr+=dr
        return False

    b=x['board']; white=x['side']=='w'; rank='1' if white else '8'
    kf=ord(x['king_from'][0])-97; rf=ord(x['rook_from'][0])-97
    if b.get(x['king_from'])!=('K' if white else 'k') or b.get(x['rook_from'])!=('R' if white else 'r'): return False
    if x['wing']=='K':
        pass
        kd,rd=6,5
    else:
        if rf>kf: return False
        kd,rd=2,3
    lo=min(kf,kd,rf,rd); hi=max(kf,kd,rf,rd)
    for f in range(lo,hi+1):
        q=chr(97+f)+rank
        if q in b and f not in (kf,rf): return False
    step=1 if kd>=kf else -1
    for f in range(kf,kd+step,step):
        if attacked(b, chr(97+f)+rank, not white): return False
    return True
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def attacked(bd, sq, by_white):
        f,r=ord(sq[0])-97,int(sq[1])-1
        def at(ff,rr):
            return bd.get(chr(97+ff)+str(rr+1)) if 0<=ff<8 and 0<=rr<8 else None
        P,N,B,R,Q,K=('PNBRQK' if by_white else 'pnbrqk')
        pr=r-1 if by_white else r+1
        if at(f-1,pr)==P or at(f+1,pr)==P: return True
        for df,dr in ((1,2),(2,1),(-1,2),(-2,1),(1,-2),(2,-1),(-1,-2),(-2,-1)):
            if at(f+df,r+dr)==N: return True
        for df in (-1,0,1):
            for dr in (-1,0,1):
                if (df or dr) and at(f+df,r+dr)==K: return True
        for df,dr,kinds in ((1,0,(R,Q)),(-1,0,(R,Q)),(0,1,(R,Q)),(0,-1,(R,Q)),(1,1,(B,Q)),(1,-1,(B,Q)),(-1,1,(B,Q)),(-1,-1,(B,Q))):
            ff,rr=f+df,r+dr
            while 0<=ff<8 and 0<=rr<8:
                v=at(ff,rr)
                if v:
                    if v in kinds: return True
                    break
                ff+=df; rr+=dr
        return False

    b=x['board']; white=x['side']=='w'; rank='1' if white else '8'
    kf=ord(x['king_from'][0])-97; rf=ord(x['rook_from'][0])-97
    if b.get(x['king_from'])!=('K' if white else 'k') or b.get(x['rook_from'])!=('R' if white else 'r'): return False
    if x['wing']=='K':
        if rf<kf-1: return False
        kd,rd=6,5
    else:
        if rf>kf: return False
        kd,rd=2,3
    lo=min(kf,kd,rf,rd); hi=max(kf,kd,rf,rd)
    for f in range(lo,hi+1):
        q=chr(97+f)+rank
        if q in b and f not in (kf,rf): return False
    step=1 if kd>=kf else -1
    for f in range(kf,kd+step,step):
        if attacked(b, chr(97+f)+rank, not white): return False
    return True
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('queenside d1 occupied',
   {'board': {'a1': 'R', 'b1': 'K', 'd1': 'N', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king destination attacked',
   {'board': {'a8': 'k', 'e1': 'K', 'g8': 'r', 'h1': 'R'},
    'king_from': 'e1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king in check',
   {'board': {'a8': 'k', 'f1': 'K', 'f8': 'r', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('black queenside c8 d8',
   {'board': {'a8': 'r', 'd8': 'k', 'e1': 'K'},
    'king_from': 'd8',
    'rook_from': 'a8',
    'side': 'b',
    'wing': 'Q'},
   True),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king c1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'N', 'c1': 'K', 'e8': 'k'},
    'king_from': 'c1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False),
  ('rook f1 king e1 kingside',
   {'board': {'e1': 'K', 'e8': 'k', 'f1': 'R'},
    'king_from': 'e1',
    'rook_from': 'f1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king b1 rook c1 f1 attacked',
   {'board': {'a8': 'k', 'b1': 'K', 'c1': 'R', 'f8': 'r'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)],
 [('king b1 rook a1 queenside',
   {'board': {'a1': 'R', 'b1': 'K', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   True),
  ('king b1 rook c1 kingside',
   {'board': {'b1': 'K', 'c1': 'R', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king f1 rook h1',
   {'board': {'e8': 'k', 'f1': 'K', 'h1': 'R'},
    'king_from': 'f1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('king g1 rook h1 stays',
   {'board': {'e8': 'k', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   True),
  ('rook destination occupied',
   {'board': {'e8': 'k', 'f1': 'B', 'g1': 'K', 'h1': 'R'},
    'king_from': 'g1',
    'rook_from': 'h1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook on wrong side',
   {'board': {'b1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'b1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('rook adjacent on a-side kingside',
   {'board': {'c1': 'R', 'd1': 'K', 'e8': 'k'},
    'king_from': 'd1',
    'rook_from': 'c1',
    'side': 'w',
    'wing': 'K'},
   False),
  ('king b1 rook a1 c1 attacked',
   {'board': {'a1': 'R', 'b1': 'K', 'c8': 'r', 'e8': 'k'},
    'king_from': 'b1',
    'rook_from': 'a1',
    'side': 'w',
    'wing': 'Q'},
   False)]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
King d1 castles "kingside" with the rook on b1.

Input: {"board", "side", "wing", "king_from", "rook_from"}; rights are assumed held. Chess960 castling: the king ends on g (kingside) or c (queenside) and the rook on f or d; the kingside rook must be on the king's h-side and the queenside rook on its a-side. Every square from the leftmost to the rightmost of king start, king end, rook start and rook end must be empty apart from the castling king and rook, and no square the king stands on or passes through (start to end inclusive) may be attacked.

split: open-access


prompt:
A black king on e5 attacked along the e-file may step back to e6.

Input: {"wk", "wr", "bk"} squares (black to move). Return the sorted squares the black king may move to: on the board, not adjacent to the white king, and not attacked by the rook along a rank or file (the white king blocks the rook; the black king's origin does not). Capturing the rook is legal when the rook is not adjacent to the white king.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def c(q): return ord(q[0])-97,int(q[1])-1
    wk,wr,bk=c(x['wk']),c(x['wr']),c(x['bk'])
    def rook_hits(t):
        pass
        if wr[0]!=t[0] and wr[1]!=t[1]: return False
        sf=(t[0]>wr[0])-(t[0]<wr[0]); sr=(t[1]>wr[1])-(t[1]<wr[1])
        f,r=wr[0]+sf,wr[1]+sr
        while (f,r)!=t:
            if (f,r)==wk: return False
            f+=sf; r+=sr
        return True
    out=[]
    for df in (-1,0,1):
        for dr in (-1,0,1):
            if not (df or dr): continue
            t=(bk[0]+df,bk[1]+dr)
            if not (0<=t[0]<8 and 0<=t[1]<8): continue
            if max(abs(t[0]-wk[0]),abs(t[1]-wk[1]))<=1: continue
            if rook_hits(t): continue
            out.append(chr(97+t[0])+str(t[1]+1))
    return sorted(out)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7'])],
 [('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def c(q): return ord(q[0])-97,int(q[1])-1
    wk,wr,bk=c(x['wk']),c(x['wr']),c(x['bk'])
    def rook_hits(t):
        if wr==t and t[0]!=bk[0] and t[1]!=bk[1]: return False
        if wr[0]!=t[0] and wr[1]!=t[1]: return False
        sf=(t[0]>wr[0])-(t[0]<wr[0]); sr=(t[1]>wr[1])-(t[1]<wr[1])
        f,r=wr[0]+sf,wr[1]+sr
        while (f,r)!=t:
            if (f,r)==wk: return False
            f+=sf; r+=sr
        return True
    out=[]
    for df in (-1,0,1):
        for dr in (-1,0,1):
            if not (df or dr): continue
            t=(bk[0]+df,bk[1]+dr)
            if not (0<=t[0]<8 and 0<=t[1]<8): continue
            if max(abs(t[0]-wk[0]),abs(t[1]-wk[1]))<=1: continue
            if rook_hits(t): continue
            out.append(chr(97+t[0])+str(t[1]+1))
    return sorted(out)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7'])],
 [('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A lone rook next to the black king is not offered as a capture.

Input: {"wk", "wr", "bk"} squares (black to move). Return the sorted squares the black king may move to: on the board, not adjacent to the white king, and not attacked by the rook along a rank or file (the white king blocks the rook; the black king's origin does not). Capturing the rook is legal when the rook is not adjacent to the white king.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def c(q): return ord(q[0])-97,int(q[1])-1
    wk,wr,bk=c(x['wk']),c(x['wr']),c(x['bk'])
    def rook_hits(t):
        if wr==t: return False
        if wr[0]!=t[0] or wr[1]!=t[1]: return False
        sf=(t[0]>wr[0])-(t[0]<wr[0]); sr=(t[1]>wr[1])-(t[1]<wr[1])
        f,r=wr[0]+sf,wr[1]+sr
        while (f,r)!=t:
            if (f,r)==wk: return False
            f+=sf; r+=sr
        return True
    out=[]
    for df in (-1,0,1):
        for dr in (-1,0,1):
            if not (df or dr): continue
            t=(bk[0]+df,bk[1]+dr)
            if not (0<=t[0]<8 and 0<=t[1]<8): continue
            if max(abs(t[0]-wk[0]),abs(t[1]-wk[1]))<=1: continue
            if rook_hits(t): continue
            out.append(chr(97+t[0])+str(t[1]+1))
    return sorted(out)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def c(q): return ord(q[0])-97,int(q[1])-1
    wk,wr,bk=c(x['wk']),c(x['wr']),c(x['bk'])
    def rook_hits(t):
        if wr==t: return False
        if wr[0]!=t[0]: return False
        sf=(t[0]>wr[0])-(t[0]<wr[0]); sr=(t[1]>wr[1])-(t[1]<wr[1])
        f,r=wr[0]+sf,wr[1]+sr
        while (f,r)!=t:
            if (f,r)==wk: return False
            f+=sf; r+=sr
        return True
    out=[]
    for df in (-1,0,1):
        for dr in (-1,0,1):
            if not (df or dr): continue
            t=(bk[0]+df,bk[1]+dr)
            if not (0<=t[0]<8 and 0<=t[1]<8): continue
            if max(abs(t[0]-wk[0]),abs(t[1]-wk[1]))<=1: continue
            if rook_hits(t): continue
            out.append(chr(97+t[0])+str(t[1]+1))
    return sorted(out)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('white king shields', {'bk': 'd5', 'wk': 'e3', 'wr': 'e1'},
   ['c4', 'c5', 'c6', 'd6', 'e5', 'e6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('rank cut', {'bk': 'd8', 'wk': 'a1', 'wr': 'h6'}, ['c7', 'c8', 'd7', 'e7', 'e8']),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])],
 [('retreat along rook file', {'bk': 'e5', 'wk': 'a1', 'wr': 'e1'},
   ['d4', 'd5', 'd6', 'f4', 'f5', 'f6']),
  ('capture undefended rook', {'bk': 'e5', 'wk': 'a1', 'wr': 'e4'}, ['d5', 'd6', 'e4', 'f5', 'f6']),
  ('capture defended rook', {'bk': 'e5', 'wk': 'e3', 'wr': 'e4'}, ['d5', 'd6', 'f5', 'f6']),
  ('diagonal rook capture', {'bk': 'e5', 'wk': 'a1', 'wr': 'f4'}, ['d5', 'd6', 'e6', 'f4']),
  ('back rank mate', {'bk': 'h8', 'wk': 'g6', 'wr': 'a8'}, []),
  ('corner king', {'bk': 'a8', 'wk': 'c3', 'wr': 'b7'}, ['b7']),
  ('rook far file', {'bk': 'e5', 'wk': 'h1', 'wr': 'c2'},
   ['d4', 'd5', 'd6', 'e4', 'e6', 'f4', 'f5', 'f6']),
  ('shield on rank', {'bk': 'e5', 'wk': 'c6', 'wr': 'a6'}, ['d4', 'e4', 'e6', 'f4', 'f5', 'f6'])]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'error:' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The black king may walk onto the rook's rank or file.

Input: {"wk", "wr", "bk"} squares (black to move). Return the sorted squares the black king may move to: on the board, not adjacent to the white king, and not attacked by the rook along a rank or file (the white king blocks the rook; the black king's origin does not). Capturing the rook is legal when the rook is not adjacent to the white king.

split: open-access


prompt:
After e1c1 the rook is placed on c1 and then overwritten by the king.

Input: {"board", "moves": [uci strings]}. Apply the moves in order and return the final board sorted by square. A king moving two files castles: the rook jumps from h to f (g-file destination) or from a to d (c-file destination). A pawn moving diagonally onto an empty square captures en passant, removing the pawn on the destination file and origin rank. A fifth character promotes to that piece in the mover's colour.

split: open-access


prompt:
Kf1-g1 moves the h1 rook to f1.

Input: {"board", "moves": [uci strings]}. Apply the moves in order and return the final board sorted by square. A king moving two files castles: the rook jumps from h to f (g-file destination) or from a to d (c-file destination). A pawn moving diagonally onto an empty square captures en passant, removing the pawn on the destination file and origin rank. A fifth character promotes to that piece in the mover's colour.

split: open-access