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

N = 1
observations = []
def solve(x):
    u=x['uci']; b=x['board']
    if len(u)<4: return 'syntax'
    fr,to,promo=u[:2],u[2:4],u[4:]
    for s in (fr,to):
        if s[0] not in 'abcdefgh' or s[1] not in '12345678': return 'syntax'
    if fr==to: return 'syntax'
    p=b.get(fr)
    if p is None: return 'no piece'
    last=(p=='P' and to[1]=='8') or (p=='p' and to[1]=='1')
    if last and not promo: return 'promotion required'
    if promo and not last: return 'unexpected promotion'
    if promo and promo not in 'qrbn': return 'bad promotion'
    return 'ok'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('simple move', {'board': {'e2': 'P'}, 'uci': 'e2e4'}, 'ok'),
  ('rank nine', {'board': {'e2': 'P'}, 'uci': 'e2e9'}, 'syntax'),
  ('rank zero', {'board': {'e2': 'P'}, 'uci': 'e2e0'}, 'syntax'),
  ('white promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8q'}, 'ok'),
  ('white needs promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8'}, 'promotion required'),
  ('black needs promotion', {'board': {'b2': 'p'}, 'uci': 'b2b1'}, 'promotion required'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax')],
 [('white promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8q'}, 'ok'),
  ('white needs promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8'}, 'promotion required'),
  ('black needs promotion', {'board': {'b2': 'p'}, 'uci': 'b2b1'}, 'promotion required'),
  ('black promotes knight', {'board': {'b2': 'p'}, 'uci': 'b2a1n'}, 'ok'),
  ('rook reaches last rank', {'board': {'a1': 'R'}, 'uci': 'a1a8'}, 'ok'),
  ('king promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8k'}, 'bad promotion'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax')],
 [('black promotes knight', {'board': {'b2': 'p'}, 'uci': 'b2a1n'}, 'ok'),
  ('rook reaches last rank', {'board': {'a1': 'R'}, 'uci': 'a1a8'}, 'ok'),
  ('king promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8k'}, 'bad promotion'),
  ('pawn promotion letter', {'board': {'c7': 'P'}, 'uci': 'c7c8p'}, 'bad promotion'),
  ('suffix on quiet move', {'board': {'g1': 'N'}, 'uci': 'g1f3q'}, 'unexpected promotion'),
  ('null move', {'board': {'e2': 'P'}, 'uci': 'e2e2'}, 'syntax'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax')],
 [('pawn promotion letter', {'board': {'c7': 'P'}, 'uci': 'c7c8p'}, 'bad promotion'),
  ('suffix on quiet move', {'board': {'g1': 'N'}, 'uci': 'g1f3q'}, 'unexpected promotion'),
  ('null move', {'board': {'e2': 'P'}, 'uci': 'e2e2'}, 'syntax'),
  ('vertical rook move', {'board': {'h1': 'R'}, 'uci': 'h1h5'}, 'ok'),
  ('empty origin', {'board': {'e2': 'P'}, 'uci': 'd2d4'}, 'no piece'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax'),
  ('castling encoding', {'board': {'e1': 'K', 'h1': 'R'}, 'uci': 'e1g1'}, 'ok')],
 [('simple move', {'board': {'e2': 'P'}, 'uci': 'e2e4'}, 'ok'),
  ('rank nine', {'board': {'e2': 'P'}, 'uci': 'e2e9'}, 'syntax'),
  ('rank zero', {'board': {'e2': 'P'}, 'uci': 'e2e0'}, 'syntax'),
  ('vertical rook move', {'board': {'h1': 'R'}, 'uci': 'h1h5'}, 'ok'),
  ('empty origin', {'board': {'e2': 'P'}, 'uci': 'd2d4'}, 'no piece'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax'),
  ('castling encoding', {'board': {'e1': 'K', 'h1': 'R'}, 'uci': 'e1g1'}, 'ok')]]
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):
    u=x['uci']; b=x['board']
    if len(u)>5: return 'syntax'
    fr,to,promo=u[:2],u[2:4],u[4:]
    for s in (fr,to):
        if s[0] not in 'abcdefgh' or s[1] not in '12345678': return 'syntax'
    if fr==to: return 'syntax'
    p=b.get(fr)
    if p is None: return 'no piece'
    last=(p=='P' and to[1]=='8') or (p=='p' and to[1]=='1')
    if last and not promo: return 'promotion required'
    if promo and not last: return 'unexpected promotion'
    if promo and promo not in 'qrbn': return 'bad promotion'
    return 'ok'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('simple move', {'board': {'e2': 'P'}, 'uci': 'e2e4'}, 'ok'),
  ('rank nine', {'board': {'e2': 'P'}, 'uci': 'e2e9'}, 'syntax'),
  ('rank zero', {'board': {'e2': 'P'}, 'uci': 'e2e0'}, 'syntax'),
  ('white promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8q'}, 'ok'),
  ('white needs promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8'}, 'promotion required'),
  ('black needs promotion', {'board': {'b2': 'p'}, 'uci': 'b2b1'}, 'promotion required'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax')],
 [('white promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8q'}, 'ok'),
  ('white needs promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8'}, 'promotion required'),
  ('black needs promotion', {'board': {'b2': 'p'}, 'uci': 'b2b1'}, 'promotion required'),
  ('black promotes knight', {'board': {'b2': 'p'}, 'uci': 'b2a1n'}, 'ok'),
  ('rook reaches last rank', {'board': {'a1': 'R'}, 'uci': 'a1a8'}, 'ok'),
  ('king promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8k'}, 'bad promotion'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax')],
 [('black promotes knight', {'board': {'b2': 'p'}, 'uci': 'b2a1n'}, 'ok'),
  ('rook reaches last rank', {'board': {'a1': 'R'}, 'uci': 'a1a8'}, 'ok'),
  ('king promotion', {'board': {'e7': 'P'}, 'uci': 'e7e8k'}, 'bad promotion'),
  ('pawn promotion letter', {'board': {'c7': 'P'}, 'uci': 'c7c8p'}, 'bad promotion'),
  ('suffix on quiet move', {'board': {'g1': 'N'}, 'uci': 'g1f3q'}, 'unexpected promotion'),
  ('null move', {'board': {'e2': 'P'}, 'uci': 'e2e2'}, 'syntax'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax')],
 [('pawn promotion letter', {'board': {'c7': 'P'}, 'uci': 'c7c8p'}, 'bad promotion'),
  ('suffix on quiet move', {'board': {'g1': 'N'}, 'uci': 'g1f3q'}, 'unexpected promotion'),
  ('null move', {'board': {'e2': 'P'}, 'uci': 'e2e2'}, 'syntax'),
  ('vertical rook move', {'board': {'h1': 'R'}, 'uci': 'h1h5'}, 'ok'),
  ('empty origin', {'board': {'e2': 'P'}, 'uci': 'd2d4'}, 'no piece'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax'),
  ('castling encoding', {'board': {'e1': 'K', 'h1': 'R'}, 'uci': 'e1g1'}, 'ok')],
 [('simple move', {'board': {'e2': 'P'}, 'uci': 'e2e4'}, 'ok'),
  ('rank nine', {'board': {'e2': 'P'}, 'uci': 'e2e9'}, 'syntax'),
  ('rank zero', {'board': {'e2': 'P'}, 'uci': 'e2e0'}, 'syntax'),
  ('vertical rook move', {'board': {'h1': 'R'}, 'uci': 'h1h5'}, 'ok'),
  ('empty origin', {'board': {'e2': 'P'}, 'uci': 'd2d4'}, 'no piece'),
  ('too long', {'board': {'e7': 'P'}, 'uci': 'e7e8qq'}, 'syntax'),
  ('too short', {'board': {'e2': 'P'}, 'uci': 'e2e'}, 'syntax'),
  ('castling encoding', {'board': {'e1': 'K', 'h1': 'R'}, 'uci': 'e1g1'}, 'ok')]]
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:
"e7e8qq" is treated as a promotion with suffix "qq".

Input: {"board", "uci": move string}. Return "ok" or a reason: "syntax" (length not 4-5, squares outside a1-h8, or from equals to), "no piece" (origin empty), "promotion required" (a white pawn reaching rank 8 or black pawn reaching rank 1 without a suffix), "unexpected promotion" (suffix on any other move), "bad promotion" (suffix not one of q, r, b, n).

split: open-access


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

N = 1
observations = []
def solve(x):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal')],
 [('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2')],
 [('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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):
    b=x['board']; white=x['white']; s=x['san'].replace('+','')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal')],
 [('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2')],
 [('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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:
"Qf7#" fails to resolve while "Qe5+" works.

Input: {"board", "white": side to move, "san"} for a non-castling move. Check suffixes + and # are ignored. Resolve the SAN to the unique origin square of a pseudo-legal move (sliders need a clear path; pawns push one or, from their start rank through an empty square, two; pawns capture one step diagonally forward). "x" requires an enemy piece on the target and its absence requires an empty target. File/rank disambiguators filter candidates. Return the origin, "ambiguous" or "illegal".

split: open-access


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

N = 1
observations = []
def solve(x):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all(ch==q[0] for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4')]]
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):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all(ch==q[1] if ch.isdigit() else True for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4')]]
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:
"R1a3" is reported illegal although the a1 rook can play it.

Input: {"board", "white": side to move, "san"} for a non-castling move. Check suffixes + and # are ignored. Resolve the SAN to the unique origin square of a pseudo-legal move (sliders need a clear path; pawns push one or, from their start rank through an empty square, two; pawns capture one step diagonally forward). "x" requires an enemy piece on the target and its absence requires an empty target. File/rank disambiguators filter candidates. Return the origin, "ambiguous" or "illegal".

split: open-access


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

N = 1
observations = []
def solve(x):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    pass
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal')],
 [('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2')],
 [('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None and occ.isupper()==white: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal')],
 [('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2')],
 [('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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:
"Nf3" onto a black pawn resolves instead of requiring "Nxf3".

Input: {"board", "white": side to move, "san"} for a non-castling move. Check suffixes + and # are ignored. Resolve the SAN to the unique origin square of a pseudo-legal move (sliders need a clear path; pawns push one or, from their start rank through an empty square, two; pawns capture one step diagonally forward). "x" requires an enemy piece on the target and its absence requires an empty target. File/rank disambiguators filter candidates. Return the origin, "ambiguous" or "illegal".

split: open-access


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

N = 1
observations = []
def solve(x):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==1 and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7')],
 [('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2')],
 [('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 5) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7')],
 [('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2')],
 [('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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:
"d5" for a black pawn on d7 is reported illegal.

Input: {"board", "white": side to move, "san"} for a non-castling move. Check suffixes + and # are ignored. Resolve the SAN to the unique origin square of a pseudo-legal move (sliders need a clear path; pawns push one or, from their start rank through an empty square, two; pawns capture one step diagonally forward). "x" requires an enemy piece on the target and its absence requires an empty target. File/rank disambiguators filter candidates. Return the origin, "ambiguous" or "illegal".

split: open-access


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

N = 1
observations = []
def solve(x):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and abs(tr-r)==1
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==1
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if len(cands)==1: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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:
"exd3" resolves for a white pawn on e4.

Input: {"board", "white": side to move, "san"} for a non-castling move. Check suffixes + and # are ignored. Resolve the SAN to the unique origin square of a pseudo-legal move (sliders need a clear path; pawns push one or, from their start rank through an empty square, two; pawns capture one step diagonally forward). "x" requires an enemy piece on the target and its absence requires an empty target. File/rank disambiguators filter candidates. Return the origin, "ambiguous" or "illegal".

split: open-access


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

N = 1
observations = []
def solve(x):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if cands: return cands[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal')],
 [('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2')],
 [('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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):
    b=x['board']; white=x['white']; s=x['san'].rstrip('+#')
    def c(q): return ord(q[0])-97,int(q[1])-1
    def n(f,r): return chr(97+f)+str(r+1)
    piece=s[0] if s[0] in 'NBRQK' else 'P'
    body=s[1:] if piece!='P' else s
    capture='x' in body
    body=body.replace('x','')
    to=body[-2:]; dis=body[:-2]
    tf,tr=c(to)
    occ=b.get(to)
    if capture and (occ is None or occ.isupper()==white): return 'illegal'
    if not capture and occ is not None: return 'illegal'
    letter=piece if white else piece.lower()
    cands=[]
    for q,v in b.items():
        if v!=letter: continue
        f,r=c(q)
        if piece=='P':
            d=1 if white else -1
            if capture:
                ok=abs(tf-f)==1 and tr-r==d
            else:
                ok=tf==f and (tr-r==d or (tr-r==2*d and r==(1 if white else 6) and n(f,r+d) not in b))
        elif piece=='N':
            ok=(abs(tf-f),abs(tr-r)) in ((1,2),(2,1))
        elif piece=='K':
            ok=max(abs(tf-f),abs(tr-r))==1
        else:
            df,dr=tf-f,tr-r
            orth=(df==0)!=(dr==0); diag=abs(df)==abs(dr) and df!=0
            ok=(piece=='R' and orth) or (piece=='B' and diag) or (piece=='Q' and (orth or diag))
            if ok:
                sf=(df>0)-(df<0); sr=(dr>0)-(dr<0)
                cf,cr=f+sf,r+sr
                while (cf,cr)!=(tf,tr):
                    if n(cf,cr) in b: ok=False
                    cf+=sf; cr+=sr
        if ok and all((ch==q[0]) if ch.isalpha() else (ch==q[1]) for ch in dis):
            cands.append(q)
    if cands: return sorted(cands)[0]
    return 'ambiguous' if cands else 'illegal'
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal')],
 [('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('check suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qe5+', 'white': True},
   'h5'),
  ('quiet move onto enemy',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2')],
 [('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('quiet move onto own',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'P', 'g1': 'N'}, 'san': 'Nf3', 'white': True},
   'illegal'),
  ('capture with x',
   {'board': {'e1': 'K', 'e8': 'k', 'f3': 'p', 'g1': 'N'}, 'san': 'Nxf3', 'white': True}, 'g1'),
  ('black double push', {'board': {'d7': 'p', 'e1': 'K', 'e8': 'k'}, 'san': 'd5', 'white': False},
   'd7'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('white double push', {'board': {'e1': 'K', 'e2': 'P', 'e8': 'k'}, 'san': 'e4', 'white': True},
   'e2'),
  ('pawn capture file',
   {'board': {'c4': 'P', 'd5': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd5', 'white': True},
   'e4'),
  ('backward pawn capture',
   {'board': {'d3': 'p', 'e1': 'K', 'e4': 'P', 'e8': 'k'}, 'san': 'exd3', 'white': True},
   'illegal'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')],
 [('knight file disambiguation',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nbd2', 'white': True}, 'b1'),
  ('knight ambiguous',
   {'board': {'b1': 'N', 'e1': 'K', 'e8': 'k', 'f3': 'N'}, 'san': 'Nd2', 'white': True},
   'ambiguous'),
  ('rook rank disambiguation',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R1a3', 'white': True}, 'a1'),
  ('rook rank disambiguation upper',
   {'board': {'a1': 'R', 'a5': 'R', 'e1': 'K', 'e8': 'k'}, 'san': 'R5a3', 'white': True}, 'a5'),
  ('mate suffix', {'board': {'e1': 'K', 'e8': 'k', 'h5': 'Q'}, 'san': 'Qf7#', 'white': True}, 'h5'),
  ('black pawn capture',
   {'board': {'d4': 'P', 'e1': 'K', 'e5': 'p', 'e8': 'k'}, 'san': 'exd4', 'white': False}, 'e5'),
  ('rook blocked candidate',
   {'board': {'a1': 'R', 'c1': 'B', 'e2': 'K', 'e8': 'k', 'h1': 'R'}, 'san': 'Rd1', 'white': True},
   'h1'),
  ('bishop simple', {'board': {'c1': 'B', 'e1': 'K', 'e8': 'k'}, 'san': 'Bg5', 'white': True},
   'c1')]]
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:
"Nd2" with knights on b1 and f3 returns one origin instead of "ambiguous".

Input: {"board", "white": side to move, "san"} for a non-castling move. Check suffixes + and # are ignored. Resolve the SAN to the unique origin square of a pseudo-legal move (sliders need a clear path; pawns push one or, from their start rank through an empty square, two; pawns capture one step diagonally forward). "x" requires an enemy piece on the target and its absence requires an empty target. File/rank disambiguators filter candidates. Return the origin, "ambiguous" or "illegal".

split: open-access