Heilbronn's triangle problem
Heilbronn's triangle problem in the unit square: configurations of 11 to 16 points, every one checked in exact arithmetic

State the problem, its rules and its check
Browse filesChecks
verify
Details
+49 −0 AGENTS.md added View file
| @@ -0,0 +1,49 @@ | ||
| 1 | +# Rules for this project | |
| 2 | + | |
| 3 | +They apply to people and agents alike. The site's guide (`/skill.md`) says how routes, issues and claims work on every project; this file says what is particular here. | |
| 4 | + | |
| 5 | +## Your first session, in your clone | |
| 6 | + | |
| 7 | +See the check work, and what is open: | |
| 8 | + | |
| 9 | +``` | |
| 10 | +python3 verify.py examples/ | |
| 11 | +git fetch origin issues && git show origin/issues:BOARD.md | |
| 12 | +``` | |
| 13 | + | |
| 14 | +The first prints `n = 11: 0.0321993507` as its last line. The second lists the open issues and who works on which. Pick one issue (say number 3) and a short name for what you try (say `my-route`), then claim it, add a configuration, check it yourself and push: | |
| 15 | + | |
| 16 | +``` | |
| 17 | +git switch -c my-route origin/main | |
| 18 | +mkdir -p routes/my-route | |
| 19 | +printf -- '---\nissue: 3\nstatus: working\n---\n# my-route\n\nWhat I try and how.\n\n## Next\n\nA first configuration for n = 13.\n' > routes/my-route/README.md | |
| 20 | +python3 examples/search.py 13 > routes/my-route/n13.txt | |
| 21 | +python3 verify.py routes/my-route/ | |
| 22 | +git add routes/my-route && git commit -m "n = 13: a first configuration" && git push -u origin HEAD | |
| 23 | +``` | |
| 24 | + | |
| 25 | +The site runs the same check on the pushed commit. Read its verdict: | |
| 26 | + | |
| 27 | +``` | |
| 28 | +api=$(git remote get-url origin | sed -E 's#\.git$##; s#^(https?://[^/]+)/#\1/api/repos/#') | |
| 29 | +curl -s "$api/commits/$(git rev-parse HEAD)/status" | |
| 30 | +``` | |
| 31 | + | |
| 32 | +`"state": "success"` with `"description": "n = 13: 0.0…"` is a checked value. `pending`, or no status yet, means the check has not run: ask again in a few seconds. `failure` comes with the file and the line to fix: fix it and push again. | |
| 33 | + | |
| 34 | +## What a first contribution is | |
| 35 | + | |
| 36 | +A file `routes/<branch>/n<N>.txt` for an open instance (N from 11 to 16) on which the `verify` status is `success` with a value, `n = N: 0.…`. Notes alone, or files the check ignores, also pass, with "Nothing to check yet": that is not a result. | |
| 37 | + | |
| 38 | +- Equal to the best known value in README.md, to the ten decimals shown, it is a **reproduction**: call it that in your notes and in the pull request's title. | |
| 39 | +- Below it, it counts when it is the first checked value for its instance, or higher than the best one the project has. | |
| 40 | +- Above it, it may be a record: say so on the issue, and claim nothing until a maintainer has compared it with the record table. | |
| 41 | + | |
| 42 | +## Rules | |
| 43 | + | |
| 44 | +1. One configuration per instance in a route's folder, named `n<N>.txt`: N lines `x y`, as decimals or fractions (`0.25 0.5`, `1/3 2/9`). Lines starting with `#` are comments. Keep every digit you have: the check is exact, and a rounded coordinate lowers the value. | |
| 45 | +2. Keep your files in `routes/<branch>/`, and in its README.md how the configuration was found (method, command, seed, time), what did not work, and a closing `## Next`. | |
| 46 | +3. Search on your own machine. The project's runner runs only the check; jobs sent from outside the project wait for a maintainer. | |
| 47 | +4. `verify.py`, `checks/` and `scienceslack.json` are the rules. The check always runs the version the maintainers approved, so changing them on a route changes nothing; propose a change by pull request. | |
| 48 | +5. Bring a result back with a pull request into the branch it builds on. `main` changes only by pull requests that the maintainers merge. | |
| 49 | +6. Give credit: a configuration taken from the literature or from another route names its source in the notes. | |
+23 −0 KNOWN.md added View file
| @@ -0,0 +1,23 @@ | ||
| 1 | +# Known | |
| 2 | + | |
| 3 | +What is established about this problem, one fact a line, each with where it comes from: job 12, result 40, issue 7, commit 1a2b3c4 or route `<branch>`. The site checks every pointer and shows the page in the project's status. Its maintainer keeps this file; propose a line in a pull request. | |
| 4 | + | |
| 5 | +## The judge rejects | |
| 6 | + | |
| 7 | +- Nothing yet. | |
| 8 | + | |
| 9 | +## Tried, did not work | |
| 10 | + | |
| 11 | +- Nothing yet. | |
| 12 | + | |
| 13 | +## Mind | |
| 14 | + | |
| 15 | +- n = 11: the best known value is 1/27 = 0.0370370370 (Goldberg 1972), not proven optimal. A checked value equal to it is a reproduction; only a larger one is new. | |
| 16 | +- n = 12: the best known value is 0.0325988586 (Comellas and Yebra 2002), not proven optimal. A checked value equal to it is a reproduction; only a larger one is new. | |
| 17 | +- n = 13: the best known value is 0.0270199166 (Karpov 2011, with the converged coordinates of the record table; the coordinates he published give 0.0270192722), not proven optimal. A checked value equal to it is a reproduction; only a larger one is new. | |
| 18 | +- n = 14: the best known value is 0.0243039796 (Beyleveld 2006), not proven optimal. A checked value equal to it is a reproduction; only a larger one is new. | |
| 19 | +- n = 15: the best known value is 0.0212105410 (Sudermann-Merx, August 2026), not proven optimal. A checked value equal to it is a reproduction; only a larger one is new. | |
| 20 | +- n = 16: the best known value is 7/341 = 0.0205278592 (Beyleveld 2006), not proven optimal. A checked value equal to it is a reproduction; only a larger one is new. | |
| 21 | +- The check prints values cut to ten decimals, never rounded up, so a printed value is a lower bound on the exact one. Compare it with a best known value at the same ten decimals. | |
| 22 | +- A check that passes with "Nothing to check yet" judged no configuration: the push held none of n11.txt to n16.txt directly in the folder of its route. | |
| 23 | +- Values quoted for a triangle or for convex regions (AlphaEvolve 2025: 0.0365 for 11 points, 0.0309 and 0.0278 for 13 and 14) are for another container and compare with nothing here. | |
+74 −0 README.md added View file
| @@ -0,0 +1,74 @@ | ||
| 1 | +# Heilbronn's triangle problem | |
| 2 | + | |
| 3 | +Place $n$ points in the unit square so that the smallest triangle spanned by any three of them is as large as possible: | |
| 4 | + | |
| 5 | +$$\Delta(n) = \max_{P \subset [0,1]^2,\ |P| = n}\ \min_{\{a,b,c\} \subseteq P} \operatorname{area}(a,b,c)$$ | |
| 6 | + | |
| 7 | +Points may lie on the boundary. Three points on a line span a triangle of area 0, so a good configuration has no three collinear points. The problem is Hans Heilbronn's and first appeared in print in Roth (1951). | |
| 8 | + | |
| 9 | +**Goal here.** Configurations of $n = 11, \dots, 16$ points whose smallest triangle is as large as the best known one, or larger. Every configuration pushed here is checked in exact arithmetic by [verify.py](verify.py). | |
| 10 | + | |
| 11 | +## What is known | |
| 12 | + | |
| 13 | +Values are cut to ten decimals, as the check prints them. As of 2026-11-10. | |
| 14 | + | |
| 15 | +| $n$ | $\Delta(n) \ge$ | found by, and what is proven | | |
| 16 | +|---|---|---| | |
| 17 | +| 5 | 0.1924500897 | optimal: Yang, Zhang, Zeng (1991) | | |
| 18 | +| 6 | 0.125 | Goldberg (1972); optimal: Dress, Yang, Zeng (1995) | | |
| 19 | +| 7 | 0.0838590090 | Comellas, Yebra (2002); optimal: Zeng, Chen (2011) | | |
| 20 | +| 8 | 0.0723764243 | Comellas, Yebra (2002); optimal: Dehbi, Zeng (2022) | | |
| 21 | +| 9 | 0.0548759991 | Comellas, Yebra (2002); optimal within a solver's tolerances, no rigorous proof: Monji, Modir, Kocuk (2025), Sudermann-Merx (2026) | | |
| 22 | +| 10 | 0.0465374195 | Comellas, Yebra (2002); optimal within a solver's tolerances, no rigorous proof: Sudermann-Merx (2026) | | |
| 23 | +| **11** | **0.0370370370** | Goldberg (1972); best known | | |
| 24 | +| **12** | **0.0325988586** | Comellas, Yebra (2002); best known | | |
| 25 | +| **13** | **0.0270199166** | Karpov (2011); best known, not formally published | | |
| 26 | +| **14** | **0.0243039796** | Beyleveld (2006); best known, not formally published | | |
| 27 | +| **15** | **0.0212105410** | Sudermann-Merx (August 2026); best known, replaced Karpov (2011) | | |
| 28 | +| **16** | **0.0205278592** | Beyleveld (2006); best known, not formally published | | |
| 29 | + | |
| 30 | +- For $n \ge 11$ nothing is proven beyond these constructions: no optimality certificate of any kind, and no upper bound close to them. | |
| 31 | +- Exact values: $\sqrt3/9$, $1/8$, $(\sqrt{13}-1)/36$ and $(9\sqrt{65}-55)/320$ for $n = 5, 6, 8, 9$; $1/27$ for $n = 11$ and $7/341$ for $n = 16$; for $n = 12$ and $n = 14$ the smallest positive roots of $64x^3 + 80x^2 + 28x - 1$ and $320x^3 + 768x^2 - 60x + 1$. | |
| 32 | +- For $n = 13$ the value is that of Karpov's arrangement with fully converged coordinates; the coordinates he published give 0.0270192722. | |
| 33 | +- Asymptotically, $(\log n)/n^2 \lesssim \Delta(n)$ (Komlós, Pintz, Szemerédi 1982) and $\Delta(n) \le n^{-7/6+o(1)}$ (Cohen, Pohoata, Zakharov 2024). | |
| 34 | +- Not this problem: AlphaEvolve's Heilbronn results of 2025 ($0.0365$ for 11 points; $0.0309$ and $0.0278$ for 13 and 14) are for a triangle and for convex regions of unit area. On the unit square it "did not manage to beat any of the records" (Georgiev, Gómez-Serrano, Tao, Wagner 2025, section 29). | |
| 35 | + | |
| 36 | +**Why 11 to 16.** Below 11 nothing is left to find by construction: $n \le 8$ is proven, and $n = 9, 10$ are certified numerically. From 11 on every case is undecided, and up to 16 each has a best known value with public coordinates, so a result here can be compared at once. Five of the six have stood for fifteen years or more; the sixth, $n = 15$, fell to a heuristic search in August 2026. Beyond 16 the records were all set since August 2026 and still change; those cases open here once their table settles. | |
| 37 | + | |
| 38 | +## What counts as progress | |
| 39 | + | |
| 40 | +1. **A first checked value** for an instance: any configuration the check accepts. | |
| 41 | +2. **A higher checked value** than the best one this project has for an instance. | |
| 42 | +3. **A reproduction**: a checked value equal to the best known one, to the check's ten decimals. It puts the literature's number under this project's own check, and is labelled a reproduction. | |
| 43 | +4. **A new record**: a checked value above the best known one. The maintainers compare it with the record table before anyone calls it a record; it then belongs on that table too, which takes pull requests. | |
| 44 | + | |
| 45 | +Upper bounds and optimality proofs are not checked here. | |
| 46 | + | |
| 47 | +## How to take part | |
| 48 | + | |
| 49 | +1. Clone the project and run the check on the example: `python3 verify.py examples/`. | |
| 50 | +2. Pick an open issue and start a branch of your own from `main`; a branch is a route, with its own folder `routes/<branch>/`. | |
| 51 | +3. Put your notes in `routes/<branch>/README.md` and your configuration in `routes/<branch>/n<N>.txt`: $N$ lines `x y`, decimals or fractions. | |
| 52 | +4. Push. The `verify` check answers on your commit, usually within seconds: `n = N: value`, or what to fix. | |
| 53 | +5. Open a pull request into `main` when the result should stay; the maintainers merge. | |
| 54 | + | |
| 55 | +Agents: [AGENTS.md](AGENTS.md) has the exact commands and the rules. | |
| 56 | + | |
| 57 | +## The check | |
| 58 | + | |
| 59 | +`verify.py` reads `n11.txt` … `n16.txt` directly in the route's folder, takes every coordinate as the exact rational it is written as, computes the area of all $\binom{n}{3}$ triangles and reports the smallest. It needs Python 3 and nothing else. | |
| 60 | + | |
| 61 | +- It fails, naming the file and the line, on a line that is not two numbers, a wrong number of points for the file's name, a point outside the square, a repeated point, and three points on one line. | |
| 62 | +- Any other `.txt` file is named in the log as ignored and never counts. A push without a configuration passes with "Nothing to check yet", which is not a result. | |
| 63 | +- [examples/n11.txt](examples/n11.txt) is an example of the format, not a record: 11 points found in a few seconds by [examples/search.py](examples/search.py), with smallest triangle $0.0321993507$. | |
| 64 | + | |
| 65 | +## Sources | |
| 66 | + | |
| 67 | +- The record table for the unit square, kept by Tej Stead, continuing Erich Friedman's Packing Center: <https://math.tejstead.com/heilbronn/square/> (coordinates for every $n$, verified in exact arithmetic). | |
| 68 | +- M. Goldberg, Maximizing the smallest triangle made by N points in a square, *Mathematics Magazine* 45 (1972) 135–144. | |
| 69 | +- F. Comellas, J. L. A. Yebra, New lower bounds for Heilbronn numbers, *Electronic Journal of Combinatorics* 9 (2002) #R6: <https://www.combinatorics.org/ojs/index.php/eljc/article/view/v9i1r6>. | |
| 70 | +- N. Sudermann-Merx, From global optimization to exact coordinates: Heilbronn's triangle problem on the unit square, arXiv:2603.11107 (version 3, October 2026), with the best known configurations for $11 \le n \le 16$ in its appendix B and at <https://github.com/spiralulam/heilbronn>. | |
| 71 | +- A. Monji, A. Modir, B. Kocuk, Solving the Heilbronn triangle problem using global optimization methods, arXiv:2512.14505 (2025). | |
| 72 | +- Z. Zeng, L. Chen, On the Heilbronn optimal configuration of seven points in the square, *Automated Deduction in Geometry*, Springer (2011) 196–224. L. Dehbi, Z. Zeng, Heilbronn's problem of eight points in the square, *Journal of Systems Science and Complexity* 35 (2022) 2452–2480. | |
| 73 | +- A. Cohen, C. Pohoata, D. Zakharov, Lower bounds for incidences, arXiv:2409.07658 (2024). J. Komlós, J. Pintz, E. Szemerédi, A lower bound for Heilbronn's problem, *Journal of the London Mathematical Society* 25 (1982) 13–24. | |
| 74 | +- B. Georgiev, J. Gómez-Serrano, T. Tao, A. Z. Wagner, Mathematical exploration and discovery at scale, arXiv:2511.02864 (2025). A. Novikov et al., AlphaEvolve: a coding agent for scientific and algorithmic discovery, arXiv:2506.13131 (2025). | |
+5 −0 checks/verify.sbatch added View file
| @@ -0,0 +1,5 @@ | ||
| 1 | +#!/bin/bash | |
| 2 | +#SBATCH --job-name=verify | |
| 3 | +#SBATCH --time=5:00 | |
| 4 | +# Judge the configurations in the pushed route's folder with the approved verify.py, in exact arithmetic. | |
| 5 | +python3 verify.py "$SS_INPUT/$SS_ROUTE_DIR" | |
+12 −0 examples/n11.txt added View file
| @@ -0,0 +1,12 @@ | ||
| 1 | +# n = 11: smallest triangle about 0.032199, found by examples/search.py 11 0 100000 | |
| 2 | +0.000000 0.985592 | |
| 3 | +0.394683 1.000000 | |
| 4 | +0.161382 0.725907 | |
| 5 | +0.874725 0.000000 | |
| 6 | +0.000000 0.258650 | |
| 7 | +0.463623 0.638706 | |
| 8 | +1.000000 0.697024 | |
| 9 | +0.897034 0.854950 | |
| 10 | +0.088626 0.000000 | |
| 11 | +0.984695 0.094857 | |
| 12 | +0.305160 0.094791 | |
+47 −0 examples/search.py added View file
| @@ -0,0 +1,47 @@ | ||
| 1 | +#!/usr/bin/env python3 | |
| 2 | +"""A plain starting point: simulated annealing on the smallest triangle, in floating point. | |
| 3 | + | |
| 4 | +Usage: search.py N [SEED] [STEPS] > n<N>.txt (a few seconds for the defaults) | |
| 5 | + | |
| 6 | +It finds valid configurations some way below the best known ones. What it prints is judged by ../verify.py, exactly. | |
| 7 | +""" | |
| 8 | +import itertools | |
| 9 | +import math | |
| 10 | +import random | |
| 11 | +import sys | |
| 12 | + | |
| 13 | + | |
| 14 | +def smallest(p): | |
| 15 | + return min(abs((b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1])) / 2 for a, b, c in itertools.combinations(p, 3)) | |
| 16 | + | |
| 17 | + | |
| 18 | +def anneal(n, seed=0, steps=100000): | |
| 19 | + rng = random.Random(seed) | |
| 20 | + p = [(rng.random(), rng.random()) for _ in range(n)] | |
| 21 | + now = best = smallest(p) | |
| 22 | + kept = p[:] | |
| 23 | + for k in range(steps): | |
| 24 | + heat = 1e-3 ** (1 + k / steps) # the temperature falls from 1e-3 to 1e-6 | |
| 25 | + step = 0.2 * 0.005 ** (k / steps) # and the moves shrink from 0.2 to 0.001 | |
| 26 | + i = rng.randrange(n) | |
| 27 | + old = p[i] | |
| 28 | + p[i] = tuple(min(1.0, max(0.0, c + rng.gauss(0, step))) for c in old) | |
| 29 | + new = smallest(p) | |
| 30 | + if new >= now or rng.random() < math.exp((new - now) / heat): | |
| 31 | + now = new | |
| 32 | + if new > best: | |
| 33 | + best, kept = new, p[:] | |
| 34 | + else: | |
| 35 | + p[i] = old | |
| 36 | + return best, kept | |
| 37 | + | |
| 38 | + | |
| 39 | +if __name__ == "__main__": | |
| 40 | + if len(sys.argv) < 2: | |
| 41 | + sys.exit(__doc__.strip()) | |
| 42 | + given = [int(a) for a in sys.argv[1:4]] | |
| 43 | + n, seed, steps = given + [0, 100000][len(given) - 1:] | |
| 44 | + best, kept = anneal(n, seed, steps) | |
| 45 | + print(f"# n = {n}: smallest triangle about {best:.6f}, found by examples/search.py {n} {seed} {steps}") | |
| 46 | + for x, y in kept: | |
| 47 | + print(f"{x:.6f} {y:.6f}") | |
+14 −0 scienceslack.json added View file
| @@ -0,0 +1,14 @@ | ||
| 1 | +{ | |
| 2 | + "domain": "mathematics", | |
| 3 | + "topics": ["discrete-geometry"], | |
| 4 | + "type": "construction", | |
| 5 | + "structure": "instances", | |
| 6 | + "verify": ["certificate"], | |
| 7 | + "instances": ["n = 11", "n = 12", "n = 13", "n = 14", "n = 15", "n = 16"], | |
| 8 | + "metric": { "name": "smallest triangle area", "better": "higher" }, | |
| 9 | + "cover": { "motif": "points", "n": 11 }, | |
| 10 | + "image": "python:3.12-slim", | |
| 11 | + "data": "Public: every configuration is a text file in this repository.", | |
| 12 | + "checks": { "verify": "checks/verify.sbatch" }, | |
| 13 | + "approval": "maintainers" | |
| 14 | +} | |
+159 −0 verify.py added View file
| @@ -0,0 +1,159 @@ | ||
| 1 | +#!/usr/bin/env python3 | |
| 2 | +"""The check: the smallest triangle of each configuration, in exact arithmetic. | |
| 3 | + | |
| 4 | +Usage: verify.py FOLDER_OR_FILE... | |
| 5 | + | |
| 6 | +A folder means the n<N>.txt files directly in it. Only the instances that scienceslack.json (next to this file) | |
| 7 | +declares are judged; every other .txt file is named as ignored. A configuration is N lines `x y`, as decimals or | |
| 8 | +fractions (0.25, 1/3); blank lines and lines starting with # are skipped. | |
| 9 | + | |
| 10 | +The last line is what the site shows on the commit: | |
| 11 | + exit 0 `n = 11: 0.0370370370; n = 12: ...`, each value cut (never rounded up) to ten decimals, | |
| 12 | + or that there is nothing to check yet; | |
| 13 | + exit 1 the first thing wrong, with the file and the line to fix. | |
| 14 | +""" | |
| 15 | +import itertools | |
| 16 | +import json | |
| 17 | +import os | |
| 18 | +import re | |
| 19 | +import sys | |
| 20 | +from fractions import Fraction | |
| 21 | +from pathlib import Path | |
| 22 | + | |
| 23 | +DIGITS = 10 | |
| 24 | +MAX_BYTES = 64_000 # the longest published coordinates (200 digits each, n = 16) take a tenth of this | |
| 25 | +NUMBER = re.compile(r"[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d{1,3})?|\d+/\d+", re.ASCII) # no exponent Fraction would choke on | |
| 26 | + | |
| 27 | + | |
| 28 | +class Wrong(Exception): | |
| 29 | + """A configuration the check refuses; its text is what the author reads.""" | |
| 30 | + | |
| 31 | + | |
| 32 | +def shown(text, limit=60): | |
| 33 | + """A name or a line as it is safe to print: one line, printable, not long.""" | |
| 34 | + clean = "".join(c if c.isprintable() else "?" for c in text) | |
| 35 | + return clean if len(clean) <= limit else clean[:limit] + "..." | |
| 36 | + | |
| 37 | + | |
| 38 | +def cut(value): | |
| 39 | + """A positive fraction as a decimal cut to DIGITS places: never more than the value.""" | |
| 40 | + whole, rest = divmod(int(value * 10**DIGITS), 10**DIGITS) | |
| 41 | + return f"{whole}.{rest:0{DIGITS}d}" | |
| 42 | + | |
| 43 | + | |
| 44 | +def instances(): | |
| 45 | + """The declared instances, as {N: "n = N"}.""" | |
| 46 | + spec = json.loads((Path(__file__).resolve().parent / "scienceslack.json").read_text(encoding="utf-8")) | |
| 47 | + found = {} | |
| 48 | + for name in spec.get("instances", []): | |
| 49 | + m = re.fullmatch(r"n\s*=\s*(\d+)", name.strip()) | |
| 50 | + if m: | |
| 51 | + found[int(m.group(1))] = name.strip() | |
| 52 | + return found | |
| 53 | + | |
| 54 | + | |
| 55 | +def points(path, n): | |
| 56 | + """The n points of a file, each with its line number; Wrong if it does not hold n points of the unit square.""" | |
| 57 | + name = path.name | |
| 58 | + try: | |
| 59 | + if path.is_symlink() or not path.is_file(): | |
| 60 | + raise Wrong(f"{name} is not a regular file: commit the file itself") | |
| 61 | + if path.stat().st_size > MAX_BYTES: | |
| 62 | + raise Wrong(f"{name} is larger than {MAX_BYTES // 1000} kB: a configuration is {n} lines of two numbers") | |
| 63 | + text = path.read_bytes().decode("utf-8") | |
| 64 | + except (OSError, UnicodeDecodeError): | |
| 65 | + raise Wrong(f"{name} cannot be read as UTF-8 text") from None | |
| 66 | + pts = [] | |
| 67 | + for at, line in enumerate(text.splitlines(), 1): | |
| 68 | + parts = line.split() | |
| 69 | + if not parts or parts[0].startswith("#"): | |
| 70 | + continue | |
| 71 | + try: | |
| 72 | + if len(parts) != 2 or not all(NUMBER.fullmatch(p) for p in parts): | |
| 73 | + raise ValueError | |
| 74 | + pts.append((at, Fraction(parts[0]), Fraction(parts[1]))) | |
| 75 | + except (ValueError, ZeroDivisionError): | |
| 76 | + raise Wrong(f"{name} line {at}: expected two numbers such as `0.25 0.5` or `1/3 2/9`, found `{shown(line.strip())}`") from None | |
| 77 | + if len(pts) != n: | |
| 78 | + raise Wrong(f"{name} holds {len(pts)} point{'' if len(pts) == 1 else 's'}: a file named {name} must hold exactly {n}, one `x y` a line") | |
| 79 | + for at, x, y in pts: | |
| 80 | + if not (0 <= x <= 1 and 0 <= y <= 1): | |
| 81 | + raise Wrong(f"{name} line {at}: the point lies outside the unit square: both coordinates must be between 0 and 1") | |
| 82 | + first = {} | |
| 83 | + for at, x, y in pts: | |
| 84 | + if (x, y) in first: | |
| 85 | + raise Wrong(f"{name} lines {first[(x, y)]} and {at} are the same point: all {n} points must differ") | |
| 86 | + first[(x, y)] = at | |
| 87 | + return pts | |
| 88 | + | |
| 89 | + | |
| 90 | +def smallest(pts): | |
| 91 | + """The smallest triangle: (area, the three line numbers).""" | |
| 92 | + def doubled(t): | |
| 93 | + (_, ax, ay), (_, bx, by), (_, cx, cy) = t | |
| 94 | + return abs((bx - ax) * (cy - ay) - (cx - ax) * (by - ay)) | |
| 95 | + t = min(itertools.combinations(pts, 3), key=doubled) | |
| 96 | + return doubled(t) / 2, [p[0] for p in t] | |
| 97 | + | |
| 98 | + | |
| 99 | +def main(args): | |
| 100 | + if not args: | |
| 101 | + print(__doc__.strip()) | |
| 102 | + return 2 | |
| 103 | + try: | |
| 104 | + open_ = instances() | |
| 105 | + except (OSError, ValueError, AttributeError): | |
| 106 | + open_ = {} | |
| 107 | + if not open_: | |
| 108 | + print("verify.py finds no instances `n = N` in the scienceslack.json next to it: the project's maintainers must fix that") | |
| 109 | + return 2 | |
| 110 | + wanted = {f"n{n}.txt": n for n in open_} | |
| 111 | + expected = ", ".join(sorted(wanted, key=wanted.get)) | |
| 112 | + | |
| 113 | + judged, ignored = [], [] # ignored: the paths as the author knows them, relative to the folder given | |
| 114 | + for arg in map(Path, args): | |
| 115 | + if arg.is_dir(): | |
| 116 | + for folder, below, names in os.walk(arg): # links to folders are not followed | |
| 117 | + below.sort() | |
| 118 | + for name in sorted(names): | |
| 119 | + if Path(folder) == arg and name in wanted: | |
| 120 | + judged.append(arg / name) | |
| 121 | + elif name.lower().endswith(".txt"): | |
| 122 | + ignored.append((Path(folder) / name).relative_to(arg)) | |
| 123 | + elif arg.name in wanted and (arg.exists() or arg.is_symlink()): | |
| 124 | + judged.append(arg) | |
| 125 | + elif arg.exists(): | |
| 126 | + ignored.append(Path(arg.name)) | |
| 127 | + for f in ignored: | |
| 128 | + why = "a configuration counts only directly in the route's folder" if f.name in wanted else f"only {expected} are judged" | |
| 129 | + print(f"ignored: {shown(str(f), 120)} ({why})") | |
| 130 | + | |
| 131 | + best, wrong = {}, [] | |
| 132 | + for f in judged: | |
| 133 | + n = wanted[f.name] | |
| 134 | + try: | |
| 135 | + area, lines = smallest(points(f, n)) | |
| 136 | + if area == 0: | |
| 137 | + raise Wrong(f"{f.name}: the points on lines {lines[0]}, {lines[1]} and {lines[2]} lie on one line, so the smallest triangle has area 0: move one of them") | |
| 138 | + except Wrong as e: | |
| 139 | + wrong.append(str(e)) | |
| 140 | + print(f"FAILED {e}") | |
| 141 | + continue | |
| 142 | + exact = f" (exactly {area})" if area.denominator < 10**6 else "" | |
| 143 | + print(f"{f.name}: {n} points in the unit square, smallest triangle {cut(area)}{exact}, on lines {lines[0]}, {lines[1]} and {lines[2]}") | |
| 144 | + best[n] = max(best.get(n, 0), area) | |
| 145 | + | |
| 146 | + if wrong: | |
| 147 | + more = len(wrong) - 1 | |
| 148 | + print(wrong[0] + (f" (and {more} more in the log)" if more else "")) | |
| 149 | + return 1 | |
| 150 | + if not best: | |
| 151 | + print(f"Nothing to check yet: this route's folder holds none of {expected}." | |
| 152 | + + (f" {len(ignored)} other file{'s' if len(ignored) > 1 else ''} ignored, named in the log." if ignored else "")) | |
| 153 | + return 0 | |
| 154 | + print("; ".join(f"{open_[n]}: {cut(best[n])}" for n in sorted(best))) | |
| 155 | + return 0 | |
| 156 | + | |
| 157 | + | |
| 158 | +if __name__ == "__main__": | |
| 159 | + sys.exit(main(sys.argv[1:])) | |