#!/usr/bin/env python3
"""Exact checks of {"n": 75, "points": [[x,y], ...]} using only Python's standard library.

Usage: python verify-no-three.py configuration.json
Both checks use integer arithmetic. Validity is independent of novelty or provenance.
The 200-point bound keeps exhaustive checking small; no network or solver is used.
"""
import hashlib
import itertools
import json
import math
import pathlib
import sys


def verify(value):
    n, points = value["n"], value["points"]
    if type(n) is not int or not 1 <= n <= 1000 or not isinstance(points, list) or len(points) > 200:
        raise ValueError("Expected integer n in 1..1000 and at most 200 points")
    if any(not isinstance(p, list) or len(p) != 2 or any(type(x) is not int or not 0 <= x < n for x in p) for p in points):
        raise ValueError("Points must be pairs of integers within the grid")
    distinct = len({tuple(p) for p in points}) == len(points)
    if not distinct:
        return {"valid": False, "reason": "duplicate points"}
    triples = sum((b[0]-a[0])*(c[1]-a[1]) == (b[1]-a[1])*(c[0]-a[0])
                  for a, b, c in itertools.combinations(points, 3))
    directions_valid = True
    for i, (x, y) in enumerate(points):
        seen = set()
        for j, (u, v) in enumerate(points):
            if i == j:
                continue
            dx, dy = u-x, v-y
            g = math.gcd(dx, dy)
            dx, dy = dx//g, dy//g
            if dx < 0 or (dx == 0 and dy < 0):
                dx, dy = -dx, -dy
            if (dx, dy) in seen:
                directions_valid = False
            seen.add((dx, dy))
    if (triples == 0) != directions_valid:
        raise AssertionError("Independent checks disagree")
    return {"n": n, "pointCount": len(points), "valid": triples == 0,
            "collinearTriples": triples, "normalizedDirectionsValid": directions_valid,
            "reaches2n": triples == 0 and len(points) == 2*n}


if __name__ == "__main__":
    if len(sys.argv) != 2:
        raise SystemExit("Usage: python verify-no-three.py configuration.json")
    raw = pathlib.Path(sys.argv[1]).read_bytes()
    result = verify(json.loads(raw))
    result["sha256"] = hashlib.sha256(raw).hexdigest()
    print(json.dumps(result, sort_keys=True))
    raise SystemExit(0 if result["valid"] else 1)
