broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    lo=0
    hi=size-1
    def code(x,y):
        c=0
        if x<lo: c|=1
        elif x>hi: c|=2
        if y<lo: c|=4
        elif y>hi: c|=8
        return c
    out=[]
    for x0,y0,x1,y1 in commands:
        x0,y0,x1,y1=[Fraction(v) for v in (x0,y0,x1,y1)]
        c0,c1=code(x0,y0),code(x1,y1)
        result=None
        for _ in range(5):
            if not (c0|c1):
                result=[int((v+Fraction(1,2))//1) for v in (x0,y0,x1,y1)]
                break
            if c0&c1: break
            c=c0 if c0 else c1
            if c&12 and y1==y0 or c&3 and x1==x0: break
            if c&8: x,y=x0+(x1-x0)*(hi-y0)/(y1-y0),Fraction(hi)
            elif c&4: x,y=x0+(x1-x0)*(lo-y0)/(y1-y0),Fraction(lo)
            elif c&2: x,y=Fraction(hi),y0+(y1-y0)*(hi-x0)/(x1-x0)
            else: x,y=Fraction(lo),y0+(y1-y0)*(lo-x0)/(x1-x0)
            if c==c0:
                x0,y0=x,y
                c0=0
            else:
                x1,y1=x,y
                c1=code(x1,y1)
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[1, 1, 4, 3]], 6), [[1, 1, 4, 3]])
    check('explicit case 1', solve([[-3, 2, 8, 2]], 6), [[0, 2, 5, 2]])
    check('explicit case 2', solve([[-2, -2, 7, 7]], 6), [[0, 0, 5, 5]])
    check('explicit case 3', solve([[-3, 0, -1, 5]], 6), [None])
    check('explicit case 4', solve([[0, 0, 5, 5]], 6), [[0, 0, 5, 5]])
    check('explicit case 5', solve([[0, 0, '5.8', 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 6), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 6), [None])
    check('explicit case 8', solve([[-2, 1, 4, 10]], 6), [[0, 4, 1, 5]])
    check('explicit case 9', solve([[0, '2.5', 5, '2.5']], 6), [[0, 3, 5, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 6), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 9, 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 12', solve([['-5', '-7/2', '10', '17/2']], 6), [[0, 1, 5, 5]])
    check('explicit case 13', solve([[2, 1, 4, 11]], 6), [[2, 1, 3, 5]])
    check('explicit case 14', solve([[3, 8, 1, -2]], 6), [[2, 5, 1, 0]])
    check('explicit case 15', solve([[8, 1, 1, 9]], 6), [[5, 4, 5, 5]])
if N == 2:
    check('explicit case 0', solve([[1, 1, 5, 4]], 7), [[1, 1, 5, 4]])
    check('explicit case 1', solve([[-3, 2, 9, 2]], 7), [[0, 2, 6, 2]])
    check('explicit case 2', solve([[-2, -2, 8, 8]], 7), [[0, 0, 6, 6]])
    check('explicit case 3', solve([[-3, 0, -1, 6]], 7), [None])
    check('explicit case 4', solve([[0, 0, 6, 6]], 7), [[0, 0, 6, 6]])
    check('explicit case 5', solve([[0, 0, '6.8', 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 7), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 7), [None])
    check('explicit case 8', solve([[-2, 1, 4, 11]], 7), [[0, 4, 1, 6]])
    check('explicit case 9', solve([[0, '2.5', 6, '2.5']], 7), [[0, 3, 6, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 7), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 10, 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 12', solve([['-6', '-9/2', '12', '21/2']], 7), [[0, 1, 6, 6]])
    check('explicit case 13', solve([[2, 1, 4, 12]], 7), [[2, 1, 3, 6]])
    check('explicit case 14', solve([[3, 9, 1, -2]], 7), [[2, 6, 1, 0]])
    check('explicit case 15', solve([[9, 1, 1, 10]], 7), [[6, 4, 5, 6]])
if N == 3:
    check('explicit case 0', solve([[1, 1, 6, 5]], 8), [[1, 1, 6, 5]])
    check('explicit case 1', solve([[-3, 2, 10, 2]], 8), [[0, 2, 7, 2]])
    check('explicit case 2', solve([[-2, -2, 9, 9]], 8), [[0, 0, 7, 7]])
    check('explicit case 3', solve([[-3, 0, -1, 7]], 8), [None])
    check('explicit case 4', solve([[0, 0, 7, 7]], 8), [[0, 0, 7, 7]])
    check('explicit case 5', solve([[0, 0, '7.8', 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 8), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 8), [None])
    check('explicit case 8', solve([[-2, 1, 4, 12]], 8), [[0, 5, 1, 7]])
    check('explicit case 9', solve([[0, '2.5', 7, '2.5']], 8), [[0, 3, 7, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 8), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 11, 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 12', solve([['-7', '-11/2', '14', '25/2']], 8), [[0, 1, 7, 7]])
    check('explicit case 13', solve([[2, 1, 4, 13]], 8), [[2, 1, 3, 7]])
    check('explicit case 14', solve([[3, 10, 1, -2]], 8), [[3, 7, 1, 0]])
    check('explicit case 15', solve([[10, 1, 1, 11]], 8), [[7, 4, 5, 7]])
if N == 4:
    check('explicit case 0', solve([[1, 1, 7, 6]], 9), [[1, 1, 7, 6]])
    check('explicit case 1', solve([[-3, 2, 11, 2]], 9), [[0, 2, 8, 2]])
    check('explicit case 2', solve([[-2, -2, 10, 10]], 9), [[0, 0, 8, 8]])
    check('explicit case 3', solve([[-3, 0, -1, 8]], 9), [None])
    check('explicit case 4', solve([[0, 0, 8, 8]], 9), [[0, 0, 8, 8]])
    check('explicit case 5', solve([[0, 0, '8.8', 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 9), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 9), [None])
    check('explicit case 8', solve([[-2, 1, 4, 13]], 9), [[0, 5, 2, 8]])
    check('explicit case 9', solve([[0, '2.5', 8, '2.5']], 9), [[0, 3, 8, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 9), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 12, 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 12', solve([['-8', '-13/2', '16', '29/2']], 9), [[0, 1, 8, 8]])
    check('explicit case 13', solve([[2, 1, 4, 14]], 9), [[2, 1, 3, 8]])
    check('explicit case 14', solve([[3, 11, 1, -2]], 9), [[3, 8, 1, 0]])
    check('explicit case 15', solve([[11, 1, 1, 12]], 9), [[8, 4, 5, 8]])
if N == 5:
    check('explicit case 0', solve([[1, 1, 8, 7]], 10), [[1, 1, 8, 7]])
    check('explicit case 1', solve([[-3, 2, 12, 2]], 10), [[0, 2, 9, 2]])
    check('explicit case 2', solve([[-2, -2, 11, 11]], 10), [[0, 0, 9, 9]])
    check('explicit case 3', solve([[-3, 0, -1, 9]], 10), [None])
    check('explicit case 4', solve([[0, 0, 9, 9]], 10), [[0, 0, 9, 9]])
    check('explicit case 5', solve([[0, 0, '9.8', 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 10), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 10), [None])
    check('explicit case 8', solve([[-2, 1, 4, 14]], 10), [[0, 5, 2, 9]])
    check('explicit case 9', solve([[0, '2.5', 9, '2.5']], 10), [[0, 3, 9, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 10), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 13, 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 12', solve([['-9', '-15/2', '18', '33/2']], 10), [[0, 1, 9, 9]])
    check('explicit case 13', solve([[2, 1, 4, 15]], 10), [[2, 1, 3, 9]])
    check('explicit case 14', solve([[3, 12, 1, -2]], 10), [[3, 9, 1, 0]])
    check('explicit case 15', solve([[12, 1, 1, 13]], 10), [[9, 4, 5, 9]])
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
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    lo=0
    hi=size-1
    def code(x,y):
        c=0
        if x<lo: c|=1
        elif x>hi: c|=2
        if y<lo: c|=4
        elif y>hi: c|=8
        return c
    out=[]
    for x0,y0,x1,y1 in commands:
        x0,y0,x1,y1=[Fraction(v) for v in (x0,y0,x1,y1)]
        c0,c1=code(x0,y0),code(x1,y1)
        result=None
        for _ in range(5):
            if not (c0|c1):
                result=[int((v+Fraction(1,2))//1) for v in (x0,y0,x1,y1)]
                break
            if c0&c1: break
            c=c0 if c0 else c1
            if c&12 and y1==y0 or c&3 and x1==x0: break
            if c&8: x,y=x0+(x1-x0)*(hi-y0)/(y1-y0),Fraction(hi)
            elif c&4: x,y=x0+(x1-x0)*(lo-y0)/(y1-y0),Fraction(lo)
            elif c&2: x,y=Fraction(hi),y0+(y1-y0)*(hi-x0)/(x1-x0)
            else: x,y=Fraction(lo),y0+(y1-y0)*(lo-x0)/(x1-x0)
            if c==c0:
                x0,y0=x,y
                c0=code(x0,y0)&~c
            else:
                x1,y1=x,y
                c1=code(x1,y1)
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[1, 1, 4, 3]], 6), [[1, 1, 4, 3]])
    check('explicit case 1', solve([[-3, 2, 8, 2]], 6), [[0, 2, 5, 2]])
    check('explicit case 2', solve([[-2, -2, 7, 7]], 6), [[0, 0, 5, 5]])
    check('explicit case 3', solve([[-3, 0, -1, 5]], 6), [None])
    check('explicit case 4', solve([[0, 0, 5, 5]], 6), [[0, 0, 5, 5]])
    check('explicit case 5', solve([[0, 0, '5.8', 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 6), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 6), [None])
    check('explicit case 8', solve([[-2, 1, 4, 10]], 6), [[0, 4, 1, 5]])
    check('explicit case 9', solve([[0, '2.5', 5, '2.5']], 6), [[0, 3, 5, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 6), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 9, 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 12', solve([['-5', '-7/2', '10', '17/2']], 6), [[0, 1, 5, 5]])
    check('explicit case 13', solve([[2, 1, 4, 11]], 6), [[2, 1, 3, 5]])
    check('explicit case 14', solve([[3, 8, 1, -2]], 6), [[2, 5, 1, 0]])
    check('explicit case 15', solve([[8, 1, 1, 9]], 6), [[5, 4, 5, 5]])
if N == 2:
    check('explicit case 0', solve([[1, 1, 5, 4]], 7), [[1, 1, 5, 4]])
    check('explicit case 1', solve([[-3, 2, 9, 2]], 7), [[0, 2, 6, 2]])
    check('explicit case 2', solve([[-2, -2, 8, 8]], 7), [[0, 0, 6, 6]])
    check('explicit case 3', solve([[-3, 0, -1, 6]], 7), [None])
    check('explicit case 4', solve([[0, 0, 6, 6]], 7), [[0, 0, 6, 6]])
    check('explicit case 5', solve([[0, 0, '6.8', 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 7), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 7), [None])
    check('explicit case 8', solve([[-2, 1, 4, 11]], 7), [[0, 4, 1, 6]])
    check('explicit case 9', solve([[0, '2.5', 6, '2.5']], 7), [[0, 3, 6, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 7), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 10, 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 12', solve([['-6', '-9/2', '12', '21/2']], 7), [[0, 1, 6, 6]])
    check('explicit case 13', solve([[2, 1, 4, 12]], 7), [[2, 1, 3, 6]])
    check('explicit case 14', solve([[3, 9, 1, -2]], 7), [[2, 6, 1, 0]])
    check('explicit case 15', solve([[9, 1, 1, 10]], 7), [[6, 4, 5, 6]])
if N == 3:
    check('explicit case 0', solve([[1, 1, 6, 5]], 8), [[1, 1, 6, 5]])
    check('explicit case 1', solve([[-3, 2, 10, 2]], 8), [[0, 2, 7, 2]])
    check('explicit case 2', solve([[-2, -2, 9, 9]], 8), [[0, 0, 7, 7]])
    check('explicit case 3', solve([[-3, 0, -1, 7]], 8), [None])
    check('explicit case 4', solve([[0, 0, 7, 7]], 8), [[0, 0, 7, 7]])
    check('explicit case 5', solve([[0, 0, '7.8', 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 8), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 8), [None])
    check('explicit case 8', solve([[-2, 1, 4, 12]], 8), [[0, 5, 1, 7]])
    check('explicit case 9', solve([[0, '2.5', 7, '2.5']], 8), [[0, 3, 7, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 8), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 11, 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 12', solve([['-7', '-11/2', '14', '25/2']], 8), [[0, 1, 7, 7]])
    check('explicit case 13', solve([[2, 1, 4, 13]], 8), [[2, 1, 3, 7]])
    check('explicit case 14', solve([[3, 10, 1, -2]], 8), [[3, 7, 1, 0]])
    check('explicit case 15', solve([[10, 1, 1, 11]], 8), [[7, 4, 5, 7]])
if N == 4:
    check('explicit case 0', solve([[1, 1, 7, 6]], 9), [[1, 1, 7, 6]])
    check('explicit case 1', solve([[-3, 2, 11, 2]], 9), [[0, 2, 8, 2]])
    check('explicit case 2', solve([[-2, -2, 10, 10]], 9), [[0, 0, 8, 8]])
    check('explicit case 3', solve([[-3, 0, -1, 8]], 9), [None])
    check('explicit case 4', solve([[0, 0, 8, 8]], 9), [[0, 0, 8, 8]])
    check('explicit case 5', solve([[0, 0, '8.8', 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 9), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 9), [None])
    check('explicit case 8', solve([[-2, 1, 4, 13]], 9), [[0, 5, 2, 8]])
    check('explicit case 9', solve([[0, '2.5', 8, '2.5']], 9), [[0, 3, 8, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 9), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 12, 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 12', solve([['-8', '-13/2', '16', '29/2']], 9), [[0, 1, 8, 8]])
    check('explicit case 13', solve([[2, 1, 4, 14]], 9), [[2, 1, 3, 8]])
    check('explicit case 14', solve([[3, 11, 1, -2]], 9), [[3, 8, 1, 0]])
    check('explicit case 15', solve([[11, 1, 1, 12]], 9), [[8, 4, 5, 8]])
if N == 5:
    check('explicit case 0', solve([[1, 1, 8, 7]], 10), [[1, 1, 8, 7]])
    check('explicit case 1', solve([[-3, 2, 12, 2]], 10), [[0, 2, 9, 2]])
    check('explicit case 2', solve([[-2, -2, 11, 11]], 10), [[0, 0, 9, 9]])
    check('explicit case 3', solve([[-3, 0, -1, 9]], 10), [None])
    check('explicit case 4', solve([[0, 0, 9, 9]], 10), [[0, 0, 9, 9]])
    check('explicit case 5', solve([[0, 0, '9.8', 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 10), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 10), [None])
    check('explicit case 8', solve([[-2, 1, 4, 14]], 10), [[0, 5, 2, 9]])
    check('explicit case 9', solve([[0, '2.5', 9, '2.5']], 10), [[0, 3, 9, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 10), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 13, 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 12', solve([['-9', '-15/2', '18', '33/2']], 10), [[0, 1, 9, 9]])
    check('explicit case 13', solve([[2, 1, 4, 15]], 10), [[2, 1, 3, 9]])
    check('explicit case 14', solve([[3, 12, 1, -2]], 10), [[3, 9, 1, 0]])
    check('explicit case 15', solve([[12, 1, 1, 13]], 10), [[9, 4, 5, 9]])
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:
After moving endpoint 0 onto a window edge its outcode is assumed to be zero, so a point pushed into a different outside region is treated as inside.

Each command is a segment [x0,y0,x1,y1] whose endpoints are integers or exact decimal/rational strings in pixel-center coordinates. The window is the closed square [0,size-1]x[0,size-1] of pixel centers. Outcodes use strict outside tests (left 1, right 2, below 4, above 8). Both codes zero accepts; a shared bit rejects; otherwise the outside endpoint (endpoint 0 first) is moved to the highest set edge in above, below, right, left order and its code is recomputed from the new point; a clip against an edge parallel to the segment is treated as a rejection. At most five iterations are allowed; an unresolved or rejected segment returns None. Accepted endpoints round half-up to integer pixels.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    lo=0
    hi=size-1
    def code(x,y):
        c=0
        if x<lo: c|=1
        elif x>hi: c|=2
        if y<lo: c|=4
        elif y>hi: c|=8
        return c
    out=[]
    for x0,y0,x1,y1 in commands:
        x0,y0,x1,y1=[Fraction(v) for v in (x0,y0,x1,y1)]
        c0,c1=code(x0,y0),code(x1,y1)
        result=None
        for _ in range(5):
            if not (c0|c1):
                result=[int(v) for v in (x0,y0,x1,y1)]
                break
            if c0&c1: break
            c=c0 if c0 else c1
            if c&12 and y1==y0 or c&3 and x1==x0: break
            if c&8: x,y=x0+(x1-x0)*(hi-y0)/(y1-y0),Fraction(hi)
            elif c&4: x,y=x0+(x1-x0)*(lo-y0)/(y1-y0),Fraction(lo)
            elif c&2: x,y=Fraction(hi),y0+(y1-y0)*(hi-x0)/(x1-x0)
            else: x,y=Fraction(lo),y0+(y1-y0)*(lo-x0)/(x1-x0)
            if c==c0:
                x0,y0=x,y
                c0=code(x0,y0)
            else:
                x1,y1=x,y
                c1=code(x1,y1)
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[1, 1, 4, 3]], 6), [[1, 1, 4, 3]])
    check('explicit case 1', solve([[-3, 2, 8, 2]], 6), [[0, 2, 5, 2]])
    check('explicit case 2', solve([[-2, -2, 7, 7]], 6), [[0, 0, 5, 5]])
    check('explicit case 3', solve([[-3, 0, -1, 5]], 6), [None])
    check('explicit case 4', solve([[0, 0, 5, 5]], 6), [[0, 0, 5, 5]])
    check('explicit case 5', solve([[0, 0, '5.8', 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 6), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 6), [None])
    check('explicit case 8', solve([[-2, 1, 4, 10]], 6), [[0, 4, 1, 5]])
    check('explicit case 9', solve([[0, '2.5', 5, '2.5']], 6), [[0, 3, 5, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 6), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 9, 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 12', solve([['-5', '-7/2', '10', '17/2']], 6), [[0, 1, 5, 5]])
    check('explicit case 13', solve([[2, 1, 4, 11]], 6), [[2, 1, 3, 5]])
    check('explicit case 14', solve([[3, 8, 1, -2]], 6), [[2, 5, 1, 0]])
    check('explicit case 15', solve([[8, 1, 1, 9]], 6), [[5, 4, 5, 5]])
if N == 2:
    check('explicit case 0', solve([[1, 1, 5, 4]], 7), [[1, 1, 5, 4]])
    check('explicit case 1', solve([[-3, 2, 9, 2]], 7), [[0, 2, 6, 2]])
    check('explicit case 2', solve([[-2, -2, 8, 8]], 7), [[0, 0, 6, 6]])
    check('explicit case 3', solve([[-3, 0, -1, 6]], 7), [None])
    check('explicit case 4', solve([[0, 0, 6, 6]], 7), [[0, 0, 6, 6]])
    check('explicit case 5', solve([[0, 0, '6.8', 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 7), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 7), [None])
    check('explicit case 8', solve([[-2, 1, 4, 11]], 7), [[0, 4, 1, 6]])
    check('explicit case 9', solve([[0, '2.5', 6, '2.5']], 7), [[0, 3, 6, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 7), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 10, 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 12', solve([['-6', '-9/2', '12', '21/2']], 7), [[0, 1, 6, 6]])
    check('explicit case 13', solve([[2, 1, 4, 12]], 7), [[2, 1, 3, 6]])
    check('explicit case 14', solve([[3, 9, 1, -2]], 7), [[2, 6, 1, 0]])
    check('explicit case 15', solve([[9, 1, 1, 10]], 7), [[6, 4, 5, 6]])
if N == 3:
    check('explicit case 0', solve([[1, 1, 6, 5]], 8), [[1, 1, 6, 5]])
    check('explicit case 1', solve([[-3, 2, 10, 2]], 8), [[0, 2, 7, 2]])
    check('explicit case 2', solve([[-2, -2, 9, 9]], 8), [[0, 0, 7, 7]])
    check('explicit case 3', solve([[-3, 0, -1, 7]], 8), [None])
    check('explicit case 4', solve([[0, 0, 7, 7]], 8), [[0, 0, 7, 7]])
    check('explicit case 5', solve([[0, 0, '7.8', 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 8), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 8), [None])
    check('explicit case 8', solve([[-2, 1, 4, 12]], 8), [[0, 5, 1, 7]])
    check('explicit case 9', solve([[0, '2.5', 7, '2.5']], 8), [[0, 3, 7, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 8), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 11, 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 12', solve([['-7', '-11/2', '14', '25/2']], 8), [[0, 1, 7, 7]])
    check('explicit case 13', solve([[2, 1, 4, 13]], 8), [[2, 1, 3, 7]])
    check('explicit case 14', solve([[3, 10, 1, -2]], 8), [[3, 7, 1, 0]])
    check('explicit case 15', solve([[10, 1, 1, 11]], 8), [[7, 4, 5, 7]])
if N == 4:
    check('explicit case 0', solve([[1, 1, 7, 6]], 9), [[1, 1, 7, 6]])
    check('explicit case 1', solve([[-3, 2, 11, 2]], 9), [[0, 2, 8, 2]])
    check('explicit case 2', solve([[-2, -2, 10, 10]], 9), [[0, 0, 8, 8]])
    check('explicit case 3', solve([[-3, 0, -1, 8]], 9), [None])
    check('explicit case 4', solve([[0, 0, 8, 8]], 9), [[0, 0, 8, 8]])
    check('explicit case 5', solve([[0, 0, '8.8', 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 9), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 9), [None])
    check('explicit case 8', solve([[-2, 1, 4, 13]], 9), [[0, 5, 2, 8]])
    check('explicit case 9', solve([[0, '2.5', 8, '2.5']], 9), [[0, 3, 8, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 9), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 12, 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 12', solve([['-8', '-13/2', '16', '29/2']], 9), [[0, 1, 8, 8]])
    check('explicit case 13', solve([[2, 1, 4, 14]], 9), [[2, 1, 3, 8]])
    check('explicit case 14', solve([[3, 11, 1, -2]], 9), [[3, 8, 1, 0]])
    check('explicit case 15', solve([[11, 1, 1, 12]], 9), [[8, 4, 5, 8]])
if N == 5:
    check('explicit case 0', solve([[1, 1, 8, 7]], 10), [[1, 1, 8, 7]])
    check('explicit case 1', solve([[-3, 2, 12, 2]], 10), [[0, 2, 9, 2]])
    check('explicit case 2', solve([[-2, -2, 11, 11]], 10), [[0, 0, 9, 9]])
    check('explicit case 3', solve([[-3, 0, -1, 9]], 10), [None])
    check('explicit case 4', solve([[0, 0, 9, 9]], 10), [[0, 0, 9, 9]])
    check('explicit case 5', solve([[0, 0, '9.8', 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 10), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 10), [None])
    check('explicit case 8', solve([[-2, 1, 4, 14]], 10), [[0, 5, 2, 9]])
    check('explicit case 9', solve([[0, '2.5', 9, '2.5']], 10), [[0, 3, 9, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 10), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 13, 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 12', solve([['-9', '-15/2', '18', '33/2']], 10), [[0, 1, 9, 9]])
    check('explicit case 13', solve([[2, 1, 4, 15]], 10), [[2, 1, 3, 9]])
    check('explicit case 14', solve([[3, 12, 1, -2]], 10), [[3, 9, 1, 0]])
    check('explicit case 15', solve([[12, 1, 1, 13]], 10), [[9, 4, 5, 9]])
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
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    lo=0
    hi=size-1
    def code(x,y):
        c=0
        if x<lo: c|=1
        elif x>hi: c|=2
        if y<lo: c|=4
        elif y>hi: c|=8
        return c
    out=[]
    for x0,y0,x1,y1 in commands:
        x0,y0,x1,y1=[Fraction(v) for v in (x0,y0,x1,y1)]
        c0,c1=code(x0,y0),code(x1,y1)
        result=None
        for _ in range(5):
            if not (c0|c1):
                result=[round(v) for v in (x0,y0,x1,y1)]
                break
            if c0&c1: break
            c=c0 if c0 else c1
            if c&12 and y1==y0 or c&3 and x1==x0: break
            if c&8: x,y=x0+(x1-x0)*(hi-y0)/(y1-y0),Fraction(hi)
            elif c&4: x,y=x0+(x1-x0)*(lo-y0)/(y1-y0),Fraction(lo)
            elif c&2: x,y=Fraction(hi),y0+(y1-y0)*(hi-x0)/(x1-x0)
            else: x,y=Fraction(lo),y0+(y1-y0)*(lo-x0)/(x1-x0)
            if c==c0:
                x0,y0=x,y
                c0=code(x0,y0)
            else:
                x1,y1=x,y
                c1=code(x1,y1)
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[1, 1, 4, 3]], 6), [[1, 1, 4, 3]])
    check('explicit case 1', solve([[-3, 2, 8, 2]], 6), [[0, 2, 5, 2]])
    check('explicit case 2', solve([[-2, -2, 7, 7]], 6), [[0, 0, 5, 5]])
    check('explicit case 3', solve([[-3, 0, -1, 5]], 6), [None])
    check('explicit case 4', solve([[0, 0, 5, 5]], 6), [[0, 0, 5, 5]])
    check('explicit case 5', solve([[0, 0, '5.8', 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 6), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 6), [None])
    check('explicit case 8', solve([[-2, 1, 4, 10]], 6), [[0, 4, 1, 5]])
    check('explicit case 9', solve([[0, '2.5', 5, '2.5']], 6), [[0, 3, 5, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 6), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 9, 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 12', solve([['-5', '-7/2', '10', '17/2']], 6), [[0, 1, 5, 5]])
    check('explicit case 13', solve([[2, 1, 4, 11]], 6), [[2, 1, 3, 5]])
    check('explicit case 14', solve([[3, 8, 1, -2]], 6), [[2, 5, 1, 0]])
    check('explicit case 15', solve([[8, 1, 1, 9]], 6), [[5, 4, 5, 5]])
if N == 2:
    check('explicit case 0', solve([[1, 1, 5, 4]], 7), [[1, 1, 5, 4]])
    check('explicit case 1', solve([[-3, 2, 9, 2]], 7), [[0, 2, 6, 2]])
    check('explicit case 2', solve([[-2, -2, 8, 8]], 7), [[0, 0, 6, 6]])
    check('explicit case 3', solve([[-3, 0, -1, 6]], 7), [None])
    check('explicit case 4', solve([[0, 0, 6, 6]], 7), [[0, 0, 6, 6]])
    check('explicit case 5', solve([[0, 0, '6.8', 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 7), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 7), [None])
    check('explicit case 8', solve([[-2, 1, 4, 11]], 7), [[0, 4, 1, 6]])
    check('explicit case 9', solve([[0, '2.5', 6, '2.5']], 7), [[0, 3, 6, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 7), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 10, 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 12', solve([['-6', '-9/2', '12', '21/2']], 7), [[0, 1, 6, 6]])
    check('explicit case 13', solve([[2, 1, 4, 12]], 7), [[2, 1, 3, 6]])
    check('explicit case 14', solve([[3, 9, 1, -2]], 7), [[2, 6, 1, 0]])
    check('explicit case 15', solve([[9, 1, 1, 10]], 7), [[6, 4, 5, 6]])
if N == 3:
    check('explicit case 0', solve([[1, 1, 6, 5]], 8), [[1, 1, 6, 5]])
    check('explicit case 1', solve([[-3, 2, 10, 2]], 8), [[0, 2, 7, 2]])
    check('explicit case 2', solve([[-2, -2, 9, 9]], 8), [[0, 0, 7, 7]])
    check('explicit case 3', solve([[-3, 0, -1, 7]], 8), [None])
    check('explicit case 4', solve([[0, 0, 7, 7]], 8), [[0, 0, 7, 7]])
    check('explicit case 5', solve([[0, 0, '7.8', 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 8), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 8), [None])
    check('explicit case 8', solve([[-2, 1, 4, 12]], 8), [[0, 5, 1, 7]])
    check('explicit case 9', solve([[0, '2.5', 7, '2.5']], 8), [[0, 3, 7, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 8), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 11, 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 12', solve([['-7', '-11/2', '14', '25/2']], 8), [[0, 1, 7, 7]])
    check('explicit case 13', solve([[2, 1, 4, 13]], 8), [[2, 1, 3, 7]])
    check('explicit case 14', solve([[3, 10, 1, -2]], 8), [[3, 7, 1, 0]])
    check('explicit case 15', solve([[10, 1, 1, 11]], 8), [[7, 4, 5, 7]])
if N == 4:
    check('explicit case 0', solve([[1, 1, 7, 6]], 9), [[1, 1, 7, 6]])
    check('explicit case 1', solve([[-3, 2, 11, 2]], 9), [[0, 2, 8, 2]])
    check('explicit case 2', solve([[-2, -2, 10, 10]], 9), [[0, 0, 8, 8]])
    check('explicit case 3', solve([[-3, 0, -1, 8]], 9), [None])
    check('explicit case 4', solve([[0, 0, 8, 8]], 9), [[0, 0, 8, 8]])
    check('explicit case 5', solve([[0, 0, '8.8', 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 9), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 9), [None])
    check('explicit case 8', solve([[-2, 1, 4, 13]], 9), [[0, 5, 2, 8]])
    check('explicit case 9', solve([[0, '2.5', 8, '2.5']], 9), [[0, 3, 8, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 9), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 12, 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 12', solve([['-8', '-13/2', '16', '29/2']], 9), [[0, 1, 8, 8]])
    check('explicit case 13', solve([[2, 1, 4, 14]], 9), [[2, 1, 3, 8]])
    check('explicit case 14', solve([[3, 11, 1, -2]], 9), [[3, 8, 1, 0]])
    check('explicit case 15', solve([[11, 1, 1, 12]], 9), [[8, 4, 5, 8]])
if N == 5:
    check('explicit case 0', solve([[1, 1, 8, 7]], 10), [[1, 1, 8, 7]])
    check('explicit case 1', solve([[-3, 2, 12, 2]], 10), [[0, 2, 9, 2]])
    check('explicit case 2', solve([[-2, -2, 11, 11]], 10), [[0, 0, 9, 9]])
    check('explicit case 3', solve([[-3, 0, -1, 9]], 10), [None])
    check('explicit case 4', solve([[0, 0, 9, 9]], 10), [[0, 0, 9, 9]])
    check('explicit case 5', solve([[0, 0, '9.8', 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 10), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 10), [None])
    check('explicit case 8', solve([[-2, 1, 4, 14]], 10), [[0, 5, 2, 9]])
    check('explicit case 9', solve([[0, '2.5', 9, '2.5']], 10), [[0, 3, 9, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 10), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 13, 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 12', solve([['-9', '-15/2', '18', '33/2']], 10), [[0, 1, 9, 9]])
    check('explicit case 13', solve([[2, 1, 4, 15]], 10), [[2, 1, 3, 9]])
    check('explicit case 14', solve([[3, 12, 1, -2]], 10), [[3, 9, 1, 0]])
    check('explicit case 15', solve([[12, 1, 1, 13]], 10), [[9, 4, 5, 9]])
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:
Accepted fractional endpoints are truncated instead of rounded half-up to the nearest pixel center.

Each command is a segment [x0,y0,x1,y1] whose endpoints are integers or exact decimal/rational strings in pixel-center coordinates. The window is the closed square [0,size-1]x[0,size-1] of pixel centers. Outcodes use strict outside tests (left 1, right 2, below 4, above 8). Both codes zero accepts; a shared bit rejects; otherwise the outside endpoint (endpoint 0 first) is moved to the highest set edge in above, below, right, left order and its code is recomputed from the new point; a clip against an edge parallel to the segment is treated as a rejection. At most five iterations are allowed; an unresolved or rejected segment returns None. Accepted endpoints round half-up to integer pixels.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    lo=0
    hi=size
    def code(x,y):
        c=0
        if x<lo: c|=1
        elif x>hi: c|=2
        if y<lo: c|=4
        elif y>hi: c|=8
        return c
    out=[]
    for x0,y0,x1,y1 in commands:
        x0,y0,x1,y1=[Fraction(v) for v in (x0,y0,x1,y1)]
        c0,c1=code(x0,y0),code(x1,y1)
        result=None
        for _ in range(5):
            if not (c0|c1):
                result=[int((v+Fraction(1,2))//1) for v in (x0,y0,x1,y1)]
                break
            if c0&c1: break
            c=c0 if c0 else c1
            if c&12 and y1==y0 or c&3 and x1==x0: break
            if c&8: x,y=x0+(x1-x0)*(hi-y0)/(y1-y0),Fraction(hi)
            elif c&4: x,y=x0+(x1-x0)*(lo-y0)/(y1-y0),Fraction(lo)
            elif c&2: x,y=Fraction(hi),y0+(y1-y0)*(hi-x0)/(x1-x0)
            else: x,y=Fraction(lo),y0+(y1-y0)*(lo-x0)/(x1-x0)
            if c==c0:
                x0,y0=x,y
                c0=code(x0,y0)
            else:
                x1,y1=x,y
                c1=code(x1,y1)
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[1, 1, 4, 3]], 6), [[1, 1, 4, 3]])
    check('explicit case 1', solve([[-3, 2, 8, 2]], 6), [[0, 2, 5, 2]])
    check('explicit case 2', solve([[-2, -2, 7, 7]], 6), [[0, 0, 5, 5]])
    check('explicit case 3', solve([[-3, 0, -1, 5]], 6), [None])
    check('explicit case 4', solve([[0, 0, 5, 5]], 6), [[0, 0, 5, 5]])
    check('explicit case 5', solve([[0, 0, '5.8', 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 6), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 6), [None])
    check('explicit case 8', solve([[-2, 1, 4, 10]], 6), [[0, 4, 1, 5]])
    check('explicit case 9', solve([[0, '2.5', 5, '2.5']], 6), [[0, 3, 5, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 6), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 9, 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 12', solve([['-5', '-7/2', '10', '17/2']], 6), [[0, 1, 5, 5]])
    check('explicit case 13', solve([[2, 1, 4, 11]], 6), [[2, 1, 3, 5]])
    check('explicit case 14', solve([[3, 8, 1, -2]], 6), [[2, 5, 1, 0]])
    check('explicit case 15', solve([[8, 1, 1, 9]], 6), [[5, 4, 5, 5]])
if N == 2:
    check('explicit case 0', solve([[1, 1, 5, 4]], 7), [[1, 1, 5, 4]])
    check('explicit case 1', solve([[-3, 2, 9, 2]], 7), [[0, 2, 6, 2]])
    check('explicit case 2', solve([[-2, -2, 8, 8]], 7), [[0, 0, 6, 6]])
    check('explicit case 3', solve([[-3, 0, -1, 6]], 7), [None])
    check('explicit case 4', solve([[0, 0, 6, 6]], 7), [[0, 0, 6, 6]])
    check('explicit case 5', solve([[0, 0, '6.8', 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 7), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 7), [None])
    check('explicit case 8', solve([[-2, 1, 4, 11]], 7), [[0, 4, 1, 6]])
    check('explicit case 9', solve([[0, '2.5', 6, '2.5']], 7), [[0, 3, 6, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 7), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 10, 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 12', solve([['-6', '-9/2', '12', '21/2']], 7), [[0, 1, 6, 6]])
    check('explicit case 13', solve([[2, 1, 4, 12]], 7), [[2, 1, 3, 6]])
    check('explicit case 14', solve([[3, 9, 1, -2]], 7), [[2, 6, 1, 0]])
    check('explicit case 15', solve([[9, 1, 1, 10]], 7), [[6, 4, 5, 6]])
if N == 3:
    check('explicit case 0', solve([[1, 1, 6, 5]], 8), [[1, 1, 6, 5]])
    check('explicit case 1', solve([[-3, 2, 10, 2]], 8), [[0, 2, 7, 2]])
    check('explicit case 2', solve([[-2, -2, 9, 9]], 8), [[0, 0, 7, 7]])
    check('explicit case 3', solve([[-3, 0, -1, 7]], 8), [None])
    check('explicit case 4', solve([[0, 0, 7, 7]], 8), [[0, 0, 7, 7]])
    check('explicit case 5', solve([[0, 0, '7.8', 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 8), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 8), [None])
    check('explicit case 8', solve([[-2, 1, 4, 12]], 8), [[0, 5, 1, 7]])
    check('explicit case 9', solve([[0, '2.5', 7, '2.5']], 8), [[0, 3, 7, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 8), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 11, 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 12', solve([['-7', '-11/2', '14', '25/2']], 8), [[0, 1, 7, 7]])
    check('explicit case 13', solve([[2, 1, 4, 13]], 8), [[2, 1, 3, 7]])
    check('explicit case 14', solve([[3, 10, 1, -2]], 8), [[3, 7, 1, 0]])
    check('explicit case 15', solve([[10, 1, 1, 11]], 8), [[7, 4, 5, 7]])
if N == 4:
    check('explicit case 0', solve([[1, 1, 7, 6]], 9), [[1, 1, 7, 6]])
    check('explicit case 1', solve([[-3, 2, 11, 2]], 9), [[0, 2, 8, 2]])
    check('explicit case 2', solve([[-2, -2, 10, 10]], 9), [[0, 0, 8, 8]])
    check('explicit case 3', solve([[-3, 0, -1, 8]], 9), [None])
    check('explicit case 4', solve([[0, 0, 8, 8]], 9), [[0, 0, 8, 8]])
    check('explicit case 5', solve([[0, 0, '8.8', 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 9), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 9), [None])
    check('explicit case 8', solve([[-2, 1, 4, 13]], 9), [[0, 5, 2, 8]])
    check('explicit case 9', solve([[0, '2.5', 8, '2.5']], 9), [[0, 3, 8, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 9), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 12, 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 12', solve([['-8', '-13/2', '16', '29/2']], 9), [[0, 1, 8, 8]])
    check('explicit case 13', solve([[2, 1, 4, 14]], 9), [[2, 1, 3, 8]])
    check('explicit case 14', solve([[3, 11, 1, -2]], 9), [[3, 8, 1, 0]])
    check('explicit case 15', solve([[11, 1, 1, 12]], 9), [[8, 4, 5, 8]])
if N == 5:
    check('explicit case 0', solve([[1, 1, 8, 7]], 10), [[1, 1, 8, 7]])
    check('explicit case 1', solve([[-3, 2, 12, 2]], 10), [[0, 2, 9, 2]])
    check('explicit case 2', solve([[-2, -2, 11, 11]], 10), [[0, 0, 9, 9]])
    check('explicit case 3', solve([[-3, 0, -1, 9]], 10), [None])
    check('explicit case 4', solve([[0, 0, 9, 9]], 10), [[0, 0, 9, 9]])
    check('explicit case 5', solve([[0, 0, '9.8', 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 10), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 10), [None])
    check('explicit case 8', solve([[-2, 1, 4, 14]], 10), [[0, 5, 2, 9]])
    check('explicit case 9', solve([[0, '2.5', 9, '2.5']], 10), [[0, 3, 9, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 10), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 13, 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 12', solve([['-9', '-15/2', '18', '33/2']], 10), [[0, 1, 9, 9]])
    check('explicit case 13', solve([[2, 1, 4, 15]], 10), [[2, 1, 3, 9]])
    check('explicit case 14', solve([[3, 12, 1, -2]], 10), [[3, 9, 1, 0]])
    check('explicit case 15', solve([[12, 1, 1, 13]], 10), [[9, 4, 5, 9]])
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
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    lo=0
    hi=size-Fraction(1,2)
    def code(x,y):
        c=0
        if x<lo: c|=1
        elif x>hi: c|=2
        if y<lo: c|=4
        elif y>hi: c|=8
        return c
    out=[]
    for x0,y0,x1,y1 in commands:
        x0,y0,x1,y1=[Fraction(v) for v in (x0,y0,x1,y1)]
        c0,c1=code(x0,y0),code(x1,y1)
        result=None
        for _ in range(5):
            if not (c0|c1):
                result=[int((v+Fraction(1,2))//1) for v in (x0,y0,x1,y1)]
                break
            if c0&c1: break
            c=c0 if c0 else c1
            if c&12 and y1==y0 or c&3 and x1==x0: break
            if c&8: x,y=x0+(x1-x0)*(hi-y0)/(y1-y0),Fraction(hi)
            elif c&4: x,y=x0+(x1-x0)*(lo-y0)/(y1-y0),Fraction(lo)
            elif c&2: x,y=Fraction(hi),y0+(y1-y0)*(hi-x0)/(x1-x0)
            else: x,y=Fraction(lo),y0+(y1-y0)*(lo-x0)/(x1-x0)
            if c==c0:
                x0,y0=x,y
                c0=code(x0,y0)
            else:
                x1,y1=x,y
                c1=code(x1,y1)
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[1, 1, 4, 3]], 6), [[1, 1, 4, 3]])
    check('explicit case 1', solve([[-3, 2, 8, 2]], 6), [[0, 2, 5, 2]])
    check('explicit case 2', solve([[-2, -2, 7, 7]], 6), [[0, 0, 5, 5]])
    check('explicit case 3', solve([[-3, 0, -1, 5]], 6), [None])
    check('explicit case 4', solve([[0, 0, 5, 5]], 6), [[0, 0, 5, 5]])
    check('explicit case 5', solve([[0, 0, '5.8', 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 6), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 6), [None])
    check('explicit case 8', solve([[-2, 1, 4, 10]], 6), [[0, 4, 1, 5]])
    check('explicit case 9', solve([[0, '2.5', 5, '2.5']], 6), [[0, 3, 5, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 6), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 9, 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 12', solve([['-5', '-7/2', '10', '17/2']], 6), [[0, 1, 5, 5]])
    check('explicit case 13', solve([[2, 1, 4, 11]], 6), [[2, 1, 3, 5]])
    check('explicit case 14', solve([[3, 8, 1, -2]], 6), [[2, 5, 1, 0]])
    check('explicit case 15', solve([[8, 1, 1, 9]], 6), [[5, 4, 5, 5]])
if N == 2:
    check('explicit case 0', solve([[1, 1, 5, 4]], 7), [[1, 1, 5, 4]])
    check('explicit case 1', solve([[-3, 2, 9, 2]], 7), [[0, 2, 6, 2]])
    check('explicit case 2', solve([[-2, -2, 8, 8]], 7), [[0, 0, 6, 6]])
    check('explicit case 3', solve([[-3, 0, -1, 6]], 7), [None])
    check('explicit case 4', solve([[0, 0, 6, 6]], 7), [[0, 0, 6, 6]])
    check('explicit case 5', solve([[0, 0, '6.8', 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 7), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 7), [None])
    check('explicit case 8', solve([[-2, 1, 4, 11]], 7), [[0, 4, 1, 6]])
    check('explicit case 9', solve([[0, '2.5', 6, '2.5']], 7), [[0, 3, 6, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 7), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 10, 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 12', solve([['-6', '-9/2', '12', '21/2']], 7), [[0, 1, 6, 6]])
    check('explicit case 13', solve([[2, 1, 4, 12]], 7), [[2, 1, 3, 6]])
    check('explicit case 14', solve([[3, 9, 1, -2]], 7), [[2, 6, 1, 0]])
    check('explicit case 15', solve([[9, 1, 1, 10]], 7), [[6, 4, 5, 6]])
if N == 3:
    check('explicit case 0', solve([[1, 1, 6, 5]], 8), [[1, 1, 6, 5]])
    check('explicit case 1', solve([[-3, 2, 10, 2]], 8), [[0, 2, 7, 2]])
    check('explicit case 2', solve([[-2, -2, 9, 9]], 8), [[0, 0, 7, 7]])
    check('explicit case 3', solve([[-3, 0, -1, 7]], 8), [None])
    check('explicit case 4', solve([[0, 0, 7, 7]], 8), [[0, 0, 7, 7]])
    check('explicit case 5', solve([[0, 0, '7.8', 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 8), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 8), [None])
    check('explicit case 8', solve([[-2, 1, 4, 12]], 8), [[0, 5, 1, 7]])
    check('explicit case 9', solve([[0, '2.5', 7, '2.5']], 8), [[0, 3, 7, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 8), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 11, 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 12', solve([['-7', '-11/2', '14', '25/2']], 8), [[0, 1, 7, 7]])
    check('explicit case 13', solve([[2, 1, 4, 13]], 8), [[2, 1, 3, 7]])
    check('explicit case 14', solve([[3, 10, 1, -2]], 8), [[3, 7, 1, 0]])
    check('explicit case 15', solve([[10, 1, 1, 11]], 8), [[7, 4, 5, 7]])
if N == 4:
    check('explicit case 0', solve([[1, 1, 7, 6]], 9), [[1, 1, 7, 6]])
    check('explicit case 1', solve([[-3, 2, 11, 2]], 9), [[0, 2, 8, 2]])
    check('explicit case 2', solve([[-2, -2, 10, 10]], 9), [[0, 0, 8, 8]])
    check('explicit case 3', solve([[-3, 0, -1, 8]], 9), [None])
    check('explicit case 4', solve([[0, 0, 8, 8]], 9), [[0, 0, 8, 8]])
    check('explicit case 5', solve([[0, 0, '8.8', 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 9), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 9), [None])
    check('explicit case 8', solve([[-2, 1, 4, 13]], 9), [[0, 5, 2, 8]])
    check('explicit case 9', solve([[0, '2.5', 8, '2.5']], 9), [[0, 3, 8, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 9), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 12, 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 12', solve([['-8', '-13/2', '16', '29/2']], 9), [[0, 1, 8, 8]])
    check('explicit case 13', solve([[2, 1, 4, 14]], 9), [[2, 1, 3, 8]])
    check('explicit case 14', solve([[3, 11, 1, -2]], 9), [[3, 8, 1, 0]])
    check('explicit case 15', solve([[11, 1, 1, 12]], 9), [[8, 4, 5, 8]])
if N == 5:
    check('explicit case 0', solve([[1, 1, 8, 7]], 10), [[1, 1, 8, 7]])
    check('explicit case 1', solve([[-3, 2, 12, 2]], 10), [[0, 2, 9, 2]])
    check('explicit case 2', solve([[-2, -2, 11, 11]], 10), [[0, 0, 9, 9]])
    check('explicit case 3', solve([[-3, 0, -1, 9]], 10), [None])
    check('explicit case 4', solve([[0, 0, 9, 9]], 10), [[0, 0, 9, 9]])
    check('explicit case 5', solve([[0, 0, '9.8', 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 10), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 10), [None])
    check('explicit case 8', solve([[-2, 1, 4, 14]], 10), [[0, 5, 2, 9]])
    check('explicit case 9', solve([[0, '2.5', 9, '2.5']], 10), [[0, 3, 9, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 10), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 13, 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 12', solve([['-9', '-15/2', '18', '33/2']], 10), [[0, 1, 9, 9]])
    check('explicit case 13', solve([[2, 1, 4, 15]], 10), [[2, 1, 3, 9]])
    check('explicit case 14', solve([[3, 12, 1, -2]], 10), [[3, 9, 1, 0]])
    check('explicit case 15', solve([[12, 1, 1, 13]], 10), [[9, 4, 5, 9]])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The window maximum uses the pixel count rather than the last pixel center, letting clipped endpoints land one pixel beyond the raster.

Each command is a segment [x0,y0,x1,y1] whose endpoints are integers or exact decimal/rational strings in pixel-center coordinates. The window is the closed square [0,size-1]x[0,size-1] of pixel centers. Outcodes use strict outside tests (left 1, right 2, below 4, above 8). Both codes zero accepts; a shared bit rejects; otherwise the outside endpoint (endpoint 0 first) is moved to the highest set edge in above, below, right, left order and its code is recomputed from the new point; a clip against an edge parallel to the segment is treated as a rejection. At most five iterations are allowed; an unresolved or rejected segment returns None. Accepted endpoints round half-up to integer pixels.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    lo=0
    hi=size-1
    def code(x,y):
        c=0
        if x<lo: c|=1
        elif x>hi: c|=2
        if y<lo: c|=4
        elif y>hi: c|=8
        return c
    out=[]
    for x0,y0,x1,y1 in commands:
        x0,y0,x1,y1=[Fraction(v) for v in (x0,y0,x1,y1)]
        c0,c1=code(x0,y0),code(x1,y1)
        result=None
        for _ in range(2):
            if not (c0|c1):
                result=[int((v+Fraction(1,2))//1) for v in (x0,y0,x1,y1)]
                break
            if c0&c1: break
            c=c0 if c0 else c1
            if c&12 and y1==y0 or c&3 and x1==x0: break
            if c&8: x,y=x0+(x1-x0)*(hi-y0)/(y1-y0),Fraction(hi)
            elif c&4: x,y=x0+(x1-x0)*(lo-y0)/(y1-y0),Fraction(lo)
            elif c&2: x,y=Fraction(hi),y0+(y1-y0)*(hi-x0)/(x1-x0)
            else: x,y=Fraction(lo),y0+(y1-y0)*(lo-x0)/(x1-x0)
            if c==c0:
                x0,y0=x,y
                c0=code(x0,y0)
            else:
                x1,y1=x,y
                c1=code(x1,y1)
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[1, 1, 4, 3]], 6), [[1, 1, 4, 3]])
    check('explicit case 1', solve([[-3, 2, 8, 2]], 6), [[0, 2, 5, 2]])
    check('explicit case 2', solve([[-2, -2, 7, 7]], 6), [[0, 0, 5, 5]])
    check('explicit case 3', solve([[-3, 0, -1, 5]], 6), [None])
    check('explicit case 4', solve([[0, 0, 5, 5]], 6), [[0, 0, 5, 5]])
    check('explicit case 5', solve([[0, 0, '5.8', 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 6), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 6), [None])
    check('explicit case 8', solve([[-2, 1, 4, 10]], 6), [[0, 4, 1, 5]])
    check('explicit case 9', solve([[0, '2.5', 5, '2.5']], 6), [[0, 3, 5, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 6), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 9, 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 12', solve([['-5', '-7/2', '10', '17/2']], 6), [[0, 1, 5, 5]])
    check('explicit case 13', solve([[2, 1, 4, 11]], 6), [[2, 1, 3, 5]])
    check('explicit case 14', solve([[3, 8, 1, -2]], 6), [[2, 5, 1, 0]])
    check('explicit case 15', solve([[8, 1, 1, 9]], 6), [[5, 4, 5, 5]])
if N == 2:
    check('explicit case 0', solve([[1, 1, 5, 4]], 7), [[1, 1, 5, 4]])
    check('explicit case 1', solve([[-3, 2, 9, 2]], 7), [[0, 2, 6, 2]])
    check('explicit case 2', solve([[-2, -2, 8, 8]], 7), [[0, 0, 6, 6]])
    check('explicit case 3', solve([[-3, 0, -1, 6]], 7), [None])
    check('explicit case 4', solve([[0, 0, 6, 6]], 7), [[0, 0, 6, 6]])
    check('explicit case 5', solve([[0, 0, '6.8', 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 7), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 7), [None])
    check('explicit case 8', solve([[-2, 1, 4, 11]], 7), [[0, 4, 1, 6]])
    check('explicit case 9', solve([[0, '2.5', 6, '2.5']], 7), [[0, 3, 6, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 7), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 10, 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 12', solve([['-6', '-9/2', '12', '21/2']], 7), [[0, 1, 6, 6]])
    check('explicit case 13', solve([[2, 1, 4, 12]], 7), [[2, 1, 3, 6]])
    check('explicit case 14', solve([[3, 9, 1, -2]], 7), [[2, 6, 1, 0]])
    check('explicit case 15', solve([[9, 1, 1, 10]], 7), [[6, 4, 5, 6]])
if N == 3:
    check('explicit case 0', solve([[1, 1, 6, 5]], 8), [[1, 1, 6, 5]])
    check('explicit case 1', solve([[-3, 2, 10, 2]], 8), [[0, 2, 7, 2]])
    check('explicit case 2', solve([[-2, -2, 9, 9]], 8), [[0, 0, 7, 7]])
    check('explicit case 3', solve([[-3, 0, -1, 7]], 8), [None])
    check('explicit case 4', solve([[0, 0, 7, 7]], 8), [[0, 0, 7, 7]])
    check('explicit case 5', solve([[0, 0, '7.8', 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 8), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 8), [None])
    check('explicit case 8', solve([[-2, 1, 4, 12]], 8), [[0, 5, 1, 7]])
    check('explicit case 9', solve([[0, '2.5', 7, '2.5']], 8), [[0, 3, 7, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 8), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 11, 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 12', solve([['-7', '-11/2', '14', '25/2']], 8), [[0, 1, 7, 7]])
    check('explicit case 13', solve([[2, 1, 4, 13]], 8), [[2, 1, 3, 7]])
    check('explicit case 14', solve([[3, 10, 1, -2]], 8), [[3, 7, 1, 0]])
    check('explicit case 15', solve([[10, 1, 1, 11]], 8), [[7, 4, 5, 7]])
if N == 4:
    check('explicit case 0', solve([[1, 1, 7, 6]], 9), [[1, 1, 7, 6]])
    check('explicit case 1', solve([[-3, 2, 11, 2]], 9), [[0, 2, 8, 2]])
    check('explicit case 2', solve([[-2, -2, 10, 10]], 9), [[0, 0, 8, 8]])
    check('explicit case 3', solve([[-3, 0, -1, 8]], 9), [None])
    check('explicit case 4', solve([[0, 0, 8, 8]], 9), [[0, 0, 8, 8]])
    check('explicit case 5', solve([[0, 0, '8.8', 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 9), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 9), [None])
    check('explicit case 8', solve([[-2, 1, 4, 13]], 9), [[0, 5, 2, 8]])
    check('explicit case 9', solve([[0, '2.5', 8, '2.5']], 9), [[0, 3, 8, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 9), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 12, 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 12', solve([['-8', '-13/2', '16', '29/2']], 9), [[0, 1, 8, 8]])
    check('explicit case 13', solve([[2, 1, 4, 14]], 9), [[2, 1, 3, 8]])
    check('explicit case 14', solve([[3, 11, 1, -2]], 9), [[3, 8, 1, 0]])
    check('explicit case 15', solve([[11, 1, 1, 12]], 9), [[8, 4, 5, 8]])
if N == 5:
    check('explicit case 0', solve([[1, 1, 8, 7]], 10), [[1, 1, 8, 7]])
    check('explicit case 1', solve([[-3, 2, 12, 2]], 10), [[0, 2, 9, 2]])
    check('explicit case 2', solve([[-2, -2, 11, 11]], 10), [[0, 0, 9, 9]])
    check('explicit case 3', solve([[-3, 0, -1, 9]], 10), [None])
    check('explicit case 4', solve([[0, 0, 9, 9]], 10), [[0, 0, 9, 9]])
    check('explicit case 5', solve([[0, 0, '9.8', 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 10), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 10), [None])
    check('explicit case 8', solve([[-2, 1, 4, 14]], 10), [[0, 5, 2, 9]])
    check('explicit case 9', solve([[0, '2.5', 9, '2.5']], 10), [[0, 3, 9, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 10), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 13, 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 12', solve([['-9', '-15/2', '18', '33/2']], 10), [[0, 1, 9, 9]])
    check('explicit case 13', solve([[2, 1, 4, 15]], 10), [[2, 1, 3, 9]])
    check('explicit case 14', solve([[3, 12, 1, -2]], 10), [[3, 9, 1, 0]])
    check('explicit case 15', solve([[12, 1, 1, 13]], 10), [[9, 4, 5, 9]])
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
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    lo=0
    hi=size-1
    def code(x,y):
        c=0
        if x<lo: c|=1
        elif x>hi: c|=2
        if y<lo: c|=4
        elif y>hi: c|=8
        return c
    out=[]
    for x0,y0,x1,y1 in commands:
        x0,y0,x1,y1=[Fraction(v) for v in (x0,y0,x1,y1)]
        c0,c1=code(x0,y0),code(x1,y1)
        result=None
        for _ in range(3):
            if not (c0|c1):
                result=[int((v+Fraction(1,2))//1) for v in (x0,y0,x1,y1)]
                break
            if c0&c1: break
            c=c0 if c0 else c1
            if c&12 and y1==y0 or c&3 and x1==x0: break
            if c&8: x,y=x0+(x1-x0)*(hi-y0)/(y1-y0),Fraction(hi)
            elif c&4: x,y=x0+(x1-x0)*(lo-y0)/(y1-y0),Fraction(lo)
            elif c&2: x,y=Fraction(hi),y0+(y1-y0)*(hi-x0)/(x1-x0)
            else: x,y=Fraction(lo),y0+(y1-y0)*(lo-x0)/(x1-x0)
            if c==c0:
                x0,y0=x,y
                c0=code(x0,y0)
            else:
                x1,y1=x,y
                c1=code(x1,y1)
        out.append(result)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[1, 1, 4, 3]], 6), [[1, 1, 4, 3]])
    check('explicit case 1', solve([[-3, 2, 8, 2]], 6), [[0, 2, 5, 2]])
    check('explicit case 2', solve([[-2, -2, 7, 7]], 6), [[0, 0, 5, 5]])
    check('explicit case 3', solve([[-3, 0, -1, 5]], 6), [None])
    check('explicit case 4', solve([[0, 0, 5, 5]], 6), [[0, 0, 5, 5]])
    check('explicit case 5', solve([[0, 0, '5.8', 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 6), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 6), [None])
    check('explicit case 8', solve([[-2, 1, 4, 10]], 6), [[0, 4, 1, 5]])
    check('explicit case 9', solve([[0, '2.5', 5, '2.5']], 6), [[0, 3, 5, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 6), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 9, 0]], 6), [[0, 0, 5, 0]])
    check('explicit case 12', solve([['-5', '-7/2', '10', '17/2']], 6), [[0, 1, 5, 5]])
    check('explicit case 13', solve([[2, 1, 4, 11]], 6), [[2, 1, 3, 5]])
    check('explicit case 14', solve([[3, 8, 1, -2]], 6), [[2, 5, 1, 0]])
    check('explicit case 15', solve([[8, 1, 1, 9]], 6), [[5, 4, 5, 5]])
if N == 2:
    check('explicit case 0', solve([[1, 1, 5, 4]], 7), [[1, 1, 5, 4]])
    check('explicit case 1', solve([[-3, 2, 9, 2]], 7), [[0, 2, 6, 2]])
    check('explicit case 2', solve([[-2, -2, 8, 8]], 7), [[0, 0, 6, 6]])
    check('explicit case 3', solve([[-3, 0, -1, 6]], 7), [None])
    check('explicit case 4', solve([[0, 0, 6, 6]], 7), [[0, 0, 6, 6]])
    check('explicit case 5', solve([[0, 0, '6.8', 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 7), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 7), [None])
    check('explicit case 8', solve([[-2, 1, 4, 11]], 7), [[0, 4, 1, 6]])
    check('explicit case 9', solve([[0, '2.5', 6, '2.5']], 7), [[0, 3, 6, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 7), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 10, 0]], 7), [[0, 0, 6, 0]])
    check('explicit case 12', solve([['-6', '-9/2', '12', '21/2']], 7), [[0, 1, 6, 6]])
    check('explicit case 13', solve([[2, 1, 4, 12]], 7), [[2, 1, 3, 6]])
    check('explicit case 14', solve([[3, 9, 1, -2]], 7), [[2, 6, 1, 0]])
    check('explicit case 15', solve([[9, 1, 1, 10]], 7), [[6, 4, 5, 6]])
if N == 3:
    check('explicit case 0', solve([[1, 1, 6, 5]], 8), [[1, 1, 6, 5]])
    check('explicit case 1', solve([[-3, 2, 10, 2]], 8), [[0, 2, 7, 2]])
    check('explicit case 2', solve([[-2, -2, 9, 9]], 8), [[0, 0, 7, 7]])
    check('explicit case 3', solve([[-3, 0, -1, 7]], 8), [None])
    check('explicit case 4', solve([[0, 0, 7, 7]], 8), [[0, 0, 7, 7]])
    check('explicit case 5', solve([[0, 0, '7.8', 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 8), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 8), [None])
    check('explicit case 8', solve([[-2, 1, 4, 12]], 8), [[0, 5, 1, 7]])
    check('explicit case 9', solve([[0, '2.5', 7, '2.5']], 8), [[0, 3, 7, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 8), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 11, 0]], 8), [[0, 0, 7, 0]])
    check('explicit case 12', solve([['-7', '-11/2', '14', '25/2']], 8), [[0, 1, 7, 7]])
    check('explicit case 13', solve([[2, 1, 4, 13]], 8), [[2, 1, 3, 7]])
    check('explicit case 14', solve([[3, 10, 1, -2]], 8), [[3, 7, 1, 0]])
    check('explicit case 15', solve([[10, 1, 1, 11]], 8), [[7, 4, 5, 7]])
if N == 4:
    check('explicit case 0', solve([[1, 1, 7, 6]], 9), [[1, 1, 7, 6]])
    check('explicit case 1', solve([[-3, 2, 11, 2]], 9), [[0, 2, 8, 2]])
    check('explicit case 2', solve([[-2, -2, 10, 10]], 9), [[0, 0, 8, 8]])
    check('explicit case 3', solve([[-3, 0, -1, 8]], 9), [None])
    check('explicit case 4', solve([[0, 0, 8, 8]], 9), [[0, 0, 8, 8]])
    check('explicit case 5', solve([[0, 0, '8.8', 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 9), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 9), [None])
    check('explicit case 8', solve([[-2, 1, 4, 13]], 9), [[0, 5, 2, 8]])
    check('explicit case 9', solve([[0, '2.5', 8, '2.5']], 9), [[0, 3, 8, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 9), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 12, 0]], 9), [[0, 0, 8, 0]])
    check('explicit case 12', solve([['-8', '-13/2', '16', '29/2']], 9), [[0, 1, 8, 8]])
    check('explicit case 13', solve([[2, 1, 4, 14]], 9), [[2, 1, 3, 8]])
    check('explicit case 14', solve([[3, 11, 1, -2]], 9), [[3, 8, 1, 0]])
    check('explicit case 15', solve([[11, 1, 1, 12]], 9), [[8, 4, 5, 8]])
if N == 5:
    check('explicit case 0', solve([[1, 1, 8, 7]], 10), [[1, 1, 8, 7]])
    check('explicit case 1', solve([[-3, 2, 12, 2]], 10), [[0, 2, 9, 2]])
    check('explicit case 2', solve([[-2, -2, 11, 11]], 10), [[0, 0, 9, 9]])
    check('explicit case 3', solve([[-3, 0, -1, 9]], 10), [None])
    check('explicit case 4', solve([[0, 0, 9, 9]], 10), [[0, 0, 9, 9]])
    check('explicit case 5', solve([[0, 0, '9.8', 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 6', solve([[1, -4, -2, 2]], 10), [None])
    check('explicit case 7', solve([[-2, 1, 3, -4]], 10), [None])
    check('explicit case 8', solve([[-2, 1, 4, 14]], 10), [[0, 5, 2, 9]])
    check('explicit case 9', solve([[0, '2.5', 9, '2.5']], 10), [[0, 3, 9, 3]])
    check('explicit case 10', solve([[0, '1.5', 2, '1.5']], 10), [[0, 2, 2, 2]])
    check('explicit case 11', solve([[0, 0, 13, 0]], 10), [[0, 0, 9, 0]])
    check('explicit case 12', solve([['-9', '-15/2', '18', '33/2']], 10), [[0, 1, 9, 9]])
    check('explicit case 13', solve([[2, 1, 4, 15]], 10), [[2, 1, 3, 9]])
    check('explicit case 14', solve([[3, 12, 1, -2]], 10), [[3, 9, 1, 0]])
    check('explicit case 15', solve([[12, 1, 1, 13]], 10), [[9, 4, 5, 9]])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The clip loop assumes one clip per endpoint, so corner-crossing segments needing two clips at each end are reported unresolved.

Each command is a segment [x0,y0,x1,y1] whose endpoints are integers or exact decimal/rational strings in pixel-center coordinates. The window is the closed square [0,size-1]x[0,size-1] of pixel centers. Outcodes use strict outside tests (left 1, right 2, below 4, above 8). Both codes zero accepts; a shared bit rejects; otherwise the outside endpoint (endpoint 0 first) is moved to the highest set edge in above, below, right, left order and its code is recomputed from the new point; a clip against an edge parallel to the segment is treated as a rejection. At most five iterations are allowed; an unresolved or rejected segment returns None. Accepted endpoints round half-up to integer pixels.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    out=[]
    for x0,y0,x1,y1,rect in commands:
        l,t,r,b=max(rect[0],0),max(rect[1],0),rect[2],min(rect[3],size-1)
        if l>r or t>b:
            out.append(None)
            continue
        dx,dy=x1-x0,y1-y0
        t0,t1=Fraction(0),Fraction(1)
        ok=True
        for p,q in ((-dx,x0-l),(dx,r-x0),(-dy,y0-t),(dy,b-y0)):
            if p==0:
                if q<0: ok=False
            else:
                u=Fraction(q,p)
                if p<0:
                    if u>t1: ok=False
                    elif u>t0: t0=u
                else:
                    if u<t0: ok=False
                    elif u<t1: t1=u
        out.append([str(t0),str(t1)] if ok else None)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[-2, 1, 7, 3, [0, 0, 6, 6]]], 6), [['2/9', '7/9']])
    check('explicit case 1', solve([[1, 1, 11, 1, [0, 0, 11, 11]]], 6), [['0', '2/5']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 6), [None])
    check('explicit case 3', solve([[0, 2, 5, 2, [0, 2, 5, 2]]], 6), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 5, 0, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 5, 5]]], 6), [['1/2', '3/4']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 5, 5]]], 6), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 10, [0, 0, 5, 5]]], 6), [['0', '3/8']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 6]]], 6), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 5, 5]]], 6), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 5, 5]]], 6), [None])
if N == 2:
    check('explicit case 0', solve([[-2, 1, 8, 3, [0, 0, 7, 7]]], 7), [['1/5', '4/5']])
    check('explicit case 1', solve([[1, 1, 12, 1, [0, 0, 12, 12]]], 7), [['0', '5/11']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 7), [None])
    check('explicit case 3', solve([[0, 2, 6, 2, [0, 2, 6, 2]]], 7), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 6, 0, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 6, 6]]], 7), [['1/2', '7/8']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 6, 6]]], 7), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 11, [0, 0, 6, 6]]], 7), [['0', '4/9']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 7]]], 7), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 6, 6]]], 7), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 6, 6]]], 7), [None])
if N == 3:
    check('explicit case 0', solve([[-2, 1, 9, 3, [0, 0, 8, 8]]], 8), [['2/11', '9/11']])
    check('explicit case 1', solve([[1, 1, 13, 1, [0, 0, 13, 13]]], 8), [['0', '1/2']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 8), [None])
    check('explicit case 3', solve([[0, 2, 7, 2, [0, 2, 7, 2]]], 8), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 7, 0, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 7, 7]]], 8), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 7, 7]]], 8), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 12, [0, 0, 7, 7]]], 8), [['0', '1/2']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 8]]], 8), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 7, 7]]], 8), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 7, 7]]], 8), [None])
if N == 4:
    check('explicit case 0', solve([[-2, 1, 10, 3, [0, 0, 9, 9]]], 9), [['1/6', '5/6']])
    check('explicit case 1', solve([[1, 1, 14, 1, [0, 0, 14, 14]]], 9), [['0', '7/13']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 9), [None])
    check('explicit case 3', solve([[0, 2, 8, 2, [0, 2, 8, 2]]], 9), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 8, 0, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 8, 8]]], 9), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 8, 8]]], 9), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 13, [0, 0, 8, 8]]], 9), [['0', '6/11']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 9]]], 9), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 8, 8]]], 9), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 8, 8]]], 9), [None])
if N == 5:
    check('explicit case 0', solve([[-2, 1, 11, 3, [0, 0, 10, 10]]], 10), [['2/13', '11/13']])
    check('explicit case 1', solve([[1, 1, 15, 1, [0, 0, 15, 15]]], 10), [['0', '4/7']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 10), [None])
    check('explicit case 3', solve([[0, 2, 9, 2, [0, 2, 9, 2]]], 10), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 9, 0, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 9, 9]]], 10), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 9, 9]]], 10), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 14, [0, 0, 9, 9]]], 10), [['0', '7/12']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 10]]], 10), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 9, 9]]], 10), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 9, 9]]], 10), [None])
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
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    out=[]
    for x0,y0,x1,y1,rect in commands:
        l,t,r,b=max(rect[0],0),max(rect[1],0),min(rect[2],size),min(rect[3],size-1)
        if l>r or t>b:
            out.append(None)
            continue
        dx,dy=x1-x0,y1-y0
        t0,t1=Fraction(0),Fraction(1)
        ok=True
        for p,q in ((-dx,x0-l),(dx,r-x0),(-dy,y0-t),(dy,b-y0)):
            if p==0:
                if q<0: ok=False
            else:
                u=Fraction(q,p)
                if p<0:
                    if u>t1: ok=False
                    elif u>t0: t0=u
                else:
                    if u<t0: ok=False
                    elif u<t1: t1=u
        out.append([str(t0),str(t1)] if ok else None)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[-2, 1, 7, 3, [0, 0, 6, 6]]], 6), [['2/9', '7/9']])
    check('explicit case 1', solve([[1, 1, 11, 1, [0, 0, 11, 11]]], 6), [['0', '2/5']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 6), [None])
    check('explicit case 3', solve([[0, 2, 5, 2, [0, 2, 5, 2]]], 6), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 5, 0, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 5, 5]]], 6), [['1/2', '3/4']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 5, 5]]], 6), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 10, [0, 0, 5, 5]]], 6), [['0', '3/8']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 6]]], 6), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 5, 5]]], 6), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 5, 5]]], 6), [None])
if N == 2:
    check('explicit case 0', solve([[-2, 1, 8, 3, [0, 0, 7, 7]]], 7), [['1/5', '4/5']])
    check('explicit case 1', solve([[1, 1, 12, 1, [0, 0, 12, 12]]], 7), [['0', '5/11']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 7), [None])
    check('explicit case 3', solve([[0, 2, 6, 2, [0, 2, 6, 2]]], 7), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 6, 0, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 6, 6]]], 7), [['1/2', '7/8']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 6, 6]]], 7), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 11, [0, 0, 6, 6]]], 7), [['0', '4/9']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 7]]], 7), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 6, 6]]], 7), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 6, 6]]], 7), [None])
if N == 3:
    check('explicit case 0', solve([[-2, 1, 9, 3, [0, 0, 8, 8]]], 8), [['2/11', '9/11']])
    check('explicit case 1', solve([[1, 1, 13, 1, [0, 0, 13, 13]]], 8), [['0', '1/2']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 8), [None])
    check('explicit case 3', solve([[0, 2, 7, 2, [0, 2, 7, 2]]], 8), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 7, 0, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 7, 7]]], 8), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 7, 7]]], 8), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 12, [0, 0, 7, 7]]], 8), [['0', '1/2']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 8]]], 8), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 7, 7]]], 8), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 7, 7]]], 8), [None])
if N == 4:
    check('explicit case 0', solve([[-2, 1, 10, 3, [0, 0, 9, 9]]], 9), [['1/6', '5/6']])
    check('explicit case 1', solve([[1, 1, 14, 1, [0, 0, 14, 14]]], 9), [['0', '7/13']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 9), [None])
    check('explicit case 3', solve([[0, 2, 8, 2, [0, 2, 8, 2]]], 9), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 8, 0, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 8, 8]]], 9), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 8, 8]]], 9), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 13, [0, 0, 8, 8]]], 9), [['0', '6/11']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 9]]], 9), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 8, 8]]], 9), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 8, 8]]], 9), [None])
if N == 5:
    check('explicit case 0', solve([[-2, 1, 11, 3, [0, 0, 10, 10]]], 10), [['2/13', '11/13']])
    check('explicit case 1', solve([[1, 1, 15, 1, [0, 0, 15, 15]]], 10), [['0', '4/7']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 10), [None])
    check('explicit case 3', solve([[0, 2, 9, 2, [0, 2, 9, 2]]], 10), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 9, 0, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 9, 9]]], 10), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 9, 9]]], 10), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 14, [0, 0, 9, 9]]], 10), [['0', '7/12']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 10]]], 10), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 9, 9]]], 10), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 9, 9]]], 10), [None])
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The clip rectangle is not intersected with the raster on the right, so parameters extend past the last pixel column.

Each command is [x0,y0,x1,y1,rect] with integer coordinates and an inclusive integer rectangle [left,top,right,bottom] that is first intersected with the pixel-center raster [0,size-1]x[0,size-1]. An empty rectangle returns None. Liang-Barsky tests left, right, top and bottom inequalities with exact rational parameters: parallel segments outside a boundary are rejected, points on a boundary are inside, entering parameters raise t0 and leaving parameters lower t1. The result is [t0,t1] as exact rational strings or None.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    out=[]
    for x0,y0,x1,y1,rect in commands:
        l,t,r,b=max(rect[0],0),max(rect[1],0),min(rect[2],size-1),min(rect[3],size-1)
        if l>r or t>b:
            out.append(None)
            continue
        dx,dy=x1-x0,y1-y0
        t0,t1=Fraction(0),Fraction(1)
        ok=True
        for p,q in ((-dx,x0-l),(dx,r-x0),(-dy,y0-t),(dy,b-y0)):
            if p==0:
                if q<0: ok=False
            else:
                u=Fraction(q,p)
                if q<0:
                    if u>t1: ok=False
                    elif u>t0: t0=u
                else:
                    if u<t0: ok=False
                    elif u<t1: t1=u
        out.append([str(t0),str(t1)] if ok else None)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[-2, 1, 7, 3, [0, 0, 6, 6]]], 6), [['2/9', '7/9']])
    check('explicit case 1', solve([[1, 1, 11, 1, [0, 0, 11, 11]]], 6), [['0', '2/5']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 6), [None])
    check('explicit case 3', solve([[0, 2, 5, 2, [0, 2, 5, 2]]], 6), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 5, 0, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 5, 5]]], 6), [['1/2', '3/4']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 5, 5]]], 6), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 10, [0, 0, 5, 5]]], 6), [['0', '3/8']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 6]]], 6), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 5, 5]]], 6), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 5, 5]]], 6), [None])
if N == 2:
    check('explicit case 0', solve([[-2, 1, 8, 3, [0, 0, 7, 7]]], 7), [['1/5', '4/5']])
    check('explicit case 1', solve([[1, 1, 12, 1, [0, 0, 12, 12]]], 7), [['0', '5/11']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 7), [None])
    check('explicit case 3', solve([[0, 2, 6, 2, [0, 2, 6, 2]]], 7), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 6, 0, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 6, 6]]], 7), [['1/2', '7/8']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 6, 6]]], 7), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 11, [0, 0, 6, 6]]], 7), [['0', '4/9']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 7]]], 7), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 6, 6]]], 7), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 6, 6]]], 7), [None])
if N == 3:
    check('explicit case 0', solve([[-2, 1, 9, 3, [0, 0, 8, 8]]], 8), [['2/11', '9/11']])
    check('explicit case 1', solve([[1, 1, 13, 1, [0, 0, 13, 13]]], 8), [['0', '1/2']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 8), [None])
    check('explicit case 3', solve([[0, 2, 7, 2, [0, 2, 7, 2]]], 8), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 7, 0, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 7, 7]]], 8), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 7, 7]]], 8), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 12, [0, 0, 7, 7]]], 8), [['0', '1/2']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 8]]], 8), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 7, 7]]], 8), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 7, 7]]], 8), [None])
if N == 4:
    check('explicit case 0', solve([[-2, 1, 10, 3, [0, 0, 9, 9]]], 9), [['1/6', '5/6']])
    check('explicit case 1', solve([[1, 1, 14, 1, [0, 0, 14, 14]]], 9), [['0', '7/13']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 9), [None])
    check('explicit case 3', solve([[0, 2, 8, 2, [0, 2, 8, 2]]], 9), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 8, 0, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 8, 8]]], 9), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 8, 8]]], 9), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 13, [0, 0, 8, 8]]], 9), [['0', '6/11']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 9]]], 9), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 8, 8]]], 9), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 8, 8]]], 9), [None])
if N == 5:
    check('explicit case 0', solve([[-2, 1, 11, 3, [0, 0, 10, 10]]], 10), [['2/13', '11/13']])
    check('explicit case 1', solve([[1, 1, 15, 1, [0, 0, 15, 15]]], 10), [['0', '4/7']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 10), [None])
    check('explicit case 3', solve([[0, 2, 9, 2, [0, 2, 9, 2]]], 10), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 9, 0, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 9, 9]]], 10), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 9, 9]]], 10), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 14, [0, 0, 9, 9]]], 10), [['0', '7/12']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 10]]], 10), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 9, 9]]], 10), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 9, 9]]], 10), [None])
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
from fractions import Fraction
N = 1
observations = []
def solve(commands, size):
    out=[]
    for x0,y0,x1,y1,rect in commands:
        l,t,r,b=max(rect[0],0),max(rect[1],0),min(rect[2],size-1),min(rect[3],size-1)
        if l>r or t>b:
            out.append(None)
            continue
        dx,dy=x1-x0,y1-y0
        t0,t1=Fraction(0),Fraction(1)
        ok=True
        for p,q in ((-dx,x0-l),(dx,r-x0),(-dy,y0-t),(dy,b-y0)):
            if p==0:
                if q<0: ok=False
            else:
                u=Fraction(q,p)
                if p<0 or q<0:
                    if u>t1: ok=False
                    elif u>t0: t0=u
                else:
                    if u<t0: ok=False
                    elif u<t1: t1=u
        out.append([str(t0),str(t1)] if ok else None)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
if N == 1:
    check('explicit case 0', solve([[-2, 1, 7, 3, [0, 0, 6, 6]]], 6), [['2/9', '7/9']])
    check('explicit case 1', solve([[1, 1, 11, 1, [0, 0, 11, 11]]], 6), [['0', '2/5']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 6), [None])
    check('explicit case 3', solve([[0, 2, 5, 2, [0, 2, 5, 2]]], 6), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 5, 0, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 5, 5]]], 6), [['1/2', '3/4']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 5, 5]]], 6), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 5, 5]]], 6), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 10, [0, 0, 5, 5]]], 6), [['0', '3/8']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 6]]], 6), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 5, 5]]], 6), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 5, 5]]], 6), [None])
if N == 2:
    check('explicit case 0', solve([[-2, 1, 8, 3, [0, 0, 7, 7]]], 7), [['1/5', '4/5']])
    check('explicit case 1', solve([[1, 1, 12, 1, [0, 0, 12, 12]]], 7), [['0', '5/11']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 7), [None])
    check('explicit case 3', solve([[0, 2, 6, 2, [0, 2, 6, 2]]], 7), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 6, 0, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 6, 6]]], 7), [['1/2', '7/8']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 6, 6]]], 7), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 6, 6]]], 7), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 11, [0, 0, 6, 6]]], 7), [['0', '4/9']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 7]]], 7), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 6, 6]]], 7), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 6, 6]]], 7), [None])
if N == 3:
    check('explicit case 0', solve([[-2, 1, 9, 3, [0, 0, 8, 8]]], 8), [['2/11', '9/11']])
    check('explicit case 1', solve([[1, 1, 13, 1, [0, 0, 13, 13]]], 8), [['0', '1/2']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 8), [None])
    check('explicit case 3', solve([[0, 2, 7, 2, [0, 2, 7, 2]]], 8), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 7, 0, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 7, 7]]], 8), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 7, 7]]], 8), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 7, 7]]], 8), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 12, [0, 0, 7, 7]]], 8), [['0', '1/2']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 8]]], 8), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 7, 7]]], 8), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 7, 7]]], 8), [None])
if N == 4:
    check('explicit case 0', solve([[-2, 1, 10, 3, [0, 0, 9, 9]]], 9), [['1/6', '5/6']])
    check('explicit case 1', solve([[1, 1, 14, 1, [0, 0, 14, 14]]], 9), [['0', '7/13']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 9), [None])
    check('explicit case 3', solve([[0, 2, 8, 2, [0, 2, 8, 2]]], 9), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 8, 0, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 8, 8]]], 9), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 8, 8]]], 9), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 8, 8]]], 9), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 13, [0, 0, 8, 8]]], 9), [['0', '6/11']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 9]]], 9), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 8, 8]]], 9), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 8, 8]]], 9), [None])
if N == 5:
    check('explicit case 0', solve([[-2, 1, 11, 3, [0, 0, 10, 10]]], 10), [['2/13', '11/13']])
    check('explicit case 1', solve([[1, 1, 15, 1, [0, 0, 15, 15]]], 10), [['0', '4/7']])
    check('explicit case 2', solve([[0, 0, 1, 1, [3, 3, 2, 5]]], 10), [None])
    check('explicit case 3', solve([[0, 2, 9, 2, [0, 2, 9, 2]]], 10), [['0', '1']])
    check('explicit case 4', solve([[0, 0, 9, 0, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 5', solve([[0, 2, 0, 2, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 6', solve([[-4, -1, 4, 7, [0, 0, 9, 9]]], 10), [['1/2', '1']])
    check('explicit case 7', solve([[1, 1, 3, 1, [0, 0, 9, 9]]], 10), [['0', '1']])
    check('explicit case 8', solve([[-1, 1, 2, 1, [0, 0, 9, 9]]], 10), [['1/3', '1']])
    check('explicit case 9', solve([[1, 2, 1, 14, [0, 0, 9, 9]]], 10), [['0', '7/12']])
    check('explicit case 10', solve([[2, -3, 5, 5, [1, 1, 4, 10]]], 10), [['1/2', '2/3']])
    check('explicit case 11', solve([[-3, -3, -1, 4, [0, 0, 9, 9]]], 10), [None])
    check('explicit case 12', solve([[-3, -2, 4, -3, [0, 0, 9, 9]]], 10), [None])
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:
Boundaries are classified as entering or leaving by the sign of the boundary distance instead of the direction term, so interior starts treat every plane as leaving.

Each command is [x0,y0,x1,y1,rect] with integer coordinates and an inclusive integer rectangle [left,top,right,bottom] that is first intersected with the pixel-center raster [0,size-1]x[0,size-1]. An empty rectangle returns None. Liang-Barsky tests left, right, top and bottom inequalities with exact rational parameters: parallel segments outside a boundary are rejected, points on a boundary are inside, entering parameters raise t0 and leaving parameters lower t1. The result is [t0,t1] as exact rational strings or None.

split: open-access