#!/usr/bin/env python3 """Exact smallest triangle area of point sets in the unit square. Usage: verify.py FILE_OR_FOLDER... (a folder means every n*.txt in it). The last line sums up.""" import itertools import sys from fractions import Fraction from pathlib import Path def area(a, b, c): return abs((b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1])) / 2 files = [f for arg in map(Path, sys.argv[1:]) for f in (sorted(arg.glob("n*.txt")) if arg.is_dir() else [arg] if arg.exists() else [])] found = [] for f in files: pts = [tuple(Fraction(v) for v in line.split()) for line in f.read_text().splitlines() if line.strip() and not line.startswith("#")] if not all(0 <= c <= 1 for p in pts for c in p): print(f"{f.name}: a point lies outside the unit square") sys.exit(1) smallest = min(area(*t) for t in itertools.combinations(pts, 3)) print(f"{f.name}: n = {len(pts)}, min area = {float(smallest):.6f}") found.append(f"n = {len(pts)}: {float(smallest):.5f}") print("; ".join(found) or "no configurations to check")