Heilbronn's triangle problem: record configurations
Place n points in a square or triangle so the smallest triangle they form is as large as possible; beat a record or prove one optimal.
| 1 | #!/usr/bin/env python3 |
| 2 | """Checks that the candidates in a folder follow the submission format in README.md: JSON shape, container, n, the |
| 3 | coordinates' form, and that every point lies in its container (exactly). It computes no areas. |
| 4 | |
| 5 | usage: python3 tools/check_format.py routes/<branch> (or one .json file) |
| 6 | """ |
| 7 | import json |
| 8 | import pathlib |
| 9 | import re |
| 10 | import sys |
| 11 | from fractions import Fraction |
| 12 | |
| 13 | SIZES = range(20, 37) |
| 14 | DECIMAL = re.compile(r"-?\d{1,6}(\.\d{1,200})?") |
| 15 | |
| 16 | |
| 17 | def errors(candidate): |
| 18 | container, n = candidate.get("container"), candidate.get("n") |
| 19 | if container not in ("square", "triangle"): |
| 20 | return ['"container" must be "square" or "triangle"'] |
| 21 | if type(n) is not int or n not in SIZES: |
| 22 | return ['"n" must be one of the integers 20 to 36'] |
| 23 | pts = candidate.get("points") |
| 24 | if not isinstance(pts, list) or len(pts) != n: |
| 25 | return [f'"points" must be a list of {n} points'] |
| 26 | out = [] |
| 27 | for i, p in enumerate(pts): |
| 28 | if not isinstance(p, list) or len(p) != 2: |
| 29 | out.append(f"point {i} is not a list [x, y]") |
| 30 | continue |
| 31 | if not all(isinstance(c, str) and DECIMAL.fullmatch(c) for c in p): |
| 32 | out.append(f"point {i}: coordinates must be strings holding plain decimals, such as \"0.125\"") |
| 33 | continue |
| 34 | x, y = map(Fraction, p) |
| 35 | if not (0 <= x <= 1 and 0 <= y <= 1 if container == "square" else x >= 0 and y >= 0 and x + y <= 1): |
| 36 | out.append(f"point {i} ({p[0]}, {p[1]}) is outside the {container}") |
| 37 | return out |
| 38 | |
| 39 | |
| 40 | if len(sys.argv) != 2: |
| 41 | sys.exit(__doc__) |
| 42 | target = pathlib.Path(sys.argv[1]) |
| 43 | if target.is_file() and target.suffix != ".json": |
| 44 | sys.exit(f"{target}: candidates are .json files") |
| 45 | files = [target] if target.is_file() else sorted(target.rglob("*.json")) |
| 46 | found = bad = 0 |
| 47 | for path in files: |
| 48 | if path.stat().st_size > 1_000_000: |
| 49 | print(f"{path}: over 1 MB") |
| 50 | bad += 1 |
| 51 | continue |
| 52 | try: |
| 53 | candidate = json.loads(path.read_text()) |
| 54 | except Exception as exc: |
| 55 | print(f"{path}: not valid JSON ({exc})") |
| 56 | bad += 1 |
| 57 | continue |
| 58 | if not isinstance(candidate, dict) or "points" not in candidate: |
| 59 | print(f'{path}: not a candidate (no "points" key); ignored') |
| 60 | continue |
| 61 | found += 1 |
| 62 | problems = errors(candidate) |
| 63 | print(f"{path}: {'; '.join(problems[:5])}" if problems else f"{path}: ok ({candidate['container']}-n{candidate['n']})") |
| 64 | bad += bool(problems) |
| 65 | print(f"{found} candidates, {bad} files with format errors") |
| 66 | sys.exit(1 if bad else 0) |
