#!/usr/bin/env python3 """Independent exact enumeration of unit circles through rational point sets. No code from either reproduced PR is used. For each non-collinear triple, solve the two linear equations for its circumcenter relative to its first point, and keep it only if its squared circumradius is exactly one. All three chord lengths must be at most two, so an exact prefilter is safe. Input is an object with a ``points`` list of distinct coordinate pairs. Coordinates are integers or strings understood by Fraction (e.g. "3/5" or "0.25"); booleans and binary floats are deliberately rejected. The CLI reads JSON decimal literals as exact strings. Extra fields, including a claimed circle count, are ignored by solve(). This is a finite rational-input reproduction oracle, not a general-position or extremal bound. Worst-case enumeration takes O(n^3) arithmetic operations, whose bit cost depends on the input coordinate sizes, and O(n^2) storage. """ from __future__ import annotations import argparse from fractions import Fraction import json from pathlib import Path import sys def _coordinate(value: object) -> Fraction: if type(value) not in (int, str): raise ValueError("coordinates must be integers or rational strings") try: return Fraction(value) except (ValueError, ZeroDivisionError) as exc: raise ValueError(f"invalid rational coordinate: {value!r}") from exc def _points(config: object) -> list[tuple[Fraction, Fraction]]: if not isinstance(config, dict) or not isinstance(config.get("points"), list): raise ValueError("input must be an object with a points list") points = [] for index, pair in enumerate(config["points"]): if not isinstance(pair, (list, tuple)) or len(pair) != 2: raise ValueError(f"point {index} must contain exactly two coordinates") points.append((_coordinate(pair[0]), _coordinate(pair[1]))) if len(set(points)) != len(points): raise ValueError("duplicate point coordinates are not a point set") return points def solve(config: object) -> dict: """Return every unit circle and its incident input indices, exactly.""" points = _points(config) n = len(points) # All endpoints on a radius-one circle are at squared distance <= 4. # Store forward adjacency sets to enumerate each eligible triple once. nearby: list[set[int]] = [set() for _ in points] for i, (ax, ay) in enumerate(points): for j in range(i + 1, n): bx, by = points[j] if (bx - ax) ** 2 + (by - ay) ** 2 <= 4: nearby[i].add(j) incident: dict[tuple[Fraction, Fraction], set[int]] = {} for i, (ax, ay) in enumerate(points): for j in sorted(nearby[i]): bx, by = points[j] ux, uy = bx - ax, by - ay half_u_norm = (ux * ux + uy * uy) / 2 for k in sorted(nearby[i] & nearby[j]): cx, cy = points[k] vx, vy = cx - ax, cy - ay determinant = ux * vy - uy * vx if determinant == 0: continue half_v_norm = (vx * vx + vy * vy) / 2 ox = (half_u_norm * vy - uy * half_v_norm) / determinant oy = (ux * half_v_norm - half_u_norm * vx) / determinant if ox * ox + oy * oy != 1: continue center = (ax + ox, ay + oy) incident.setdefault(center, set()).update((i, j, k)) # A circle with >=3 distinct points has no collinear triple, and every # one of its points participates in a triple, so these unions are its # complete incidence lists; no second scan or approximate test is used. circles = [ {"center": [str(x), str(y)], "point_indices": sorted(incident[(x, y)])} for x, y in sorted(incident) ] return {"n": n, "unit_circles": len(circles), "circles": circles} def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("configuration", type=Path) args = parser.parse_args() try: config = json.loads(args.configuration.read_text(), parse_float=str) result = solve(config) except (OSError, ValueError) as exc: print(f"ERROR: {exc}", file=sys.stderr) return 1 print(json.dumps(result, indent=2)) return 0 if __name__ == "__main__": raise SystemExit(main())