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

| 1 | """Count the fewest original points that must move to reach a target area.""" |
| 2 | |
| 3 | import argparse |
| 4 | from fractions import Fraction |
| 5 | from itertools import combinations |
| 6 | from pathlib import Path |
| 7 | |
| 8 | |
| 9 | def blocking_triangles(points, target): |
| 10 | masks = [] |
| 11 | for i, j, k in combinations(range(len(points)), 3): |
| 12 | (ax, ay), (bx, by), (cx, cy) = points[i], points[j], points[k] |
| 13 | area = abs((bx - ax) * (cy - ay) - (cx - ax) * (by - ay)) / 2 |
| 14 | if area < target: |
| 15 | masks.append((1 << i) | (1 << j) | (1 << k)) |
| 16 | return masks |
| 17 | |
| 18 | |
| 19 | def minimum_covers(n, masks): |
| 20 | for count in range(n + 1): |
| 21 | covers = [] |
| 22 | for moved in combinations(range(n), count): |
| 23 | cover = sum(1 << i for i in moved) |
| 24 | if all(cover & mask for mask in masks): |
| 25 | covers.append(tuple(i + 1 for i in moved)) |
| 26 | if covers: |
| 27 | return covers |
| 28 | raise ValueError("no cover") |
| 29 | |
| 30 | |
| 31 | def main(): |
| 32 | parser = argparse.ArgumentParser(description=__doc__) |
| 33 | parser.add_argument("coordinates", type=Path) |
| 34 | parser.add_argument("target", type=Fraction) |
| 35 | args = parser.parse_args() |
| 36 | points = [tuple(map(Fraction, line.split())) for line in args.coordinates.read_text().splitlines() |
| 37 | if line.strip() and not line.lstrip().startswith("#")] |
| 38 | masks = blocking_triangles(points, args.target) |
| 39 | covers = minimum_covers(len(points), masks) |
| 40 | print(f"{len(masks)} triangles below the target; at least {len(covers[0])} original points must move.") |
| 41 | print(f"{len(covers)} minimum covers, 1-based indices: {covers}") |
| 42 | |
| 43 | |
| 44 | if __name__ == "__main__": |
| 45 | main() |