Read the checking program (76 lines of Python)
"""Check distinct grid points for five points on a common sphere or plane."""
import json
import sys
from itertools import combinations
MAX_BYTES = 480
GRID = 10
DIMENSION = 3
MAX_POINTS = 40
def read_points(data):
lines = data.splitlines()
if not 1 <= len(lines) <= MAX_POINTS:
raise ValueError("The submission has the wrong number of points.")
points = []
for line in lines:
tokens = line.split()
if len(tokens) != DIMENSION:
raise ValueError("Each point must have exactly the specified number of coordinates.")
if any(len(t) > len(str(GRID - 1)) or not t.isdigit() for t in tokens):
raise ValueError("Coordinates must be nonnegative decimal integers within the grid.")
point = tuple(map(int, tokens))
if any(x >= GRID for x in point):
raise ValueError("A coordinate is outside the grid.")
points.append(point)
if len(set(points)) != len(points):
raise ValueError("The same point occurs more than once.")
return points
def det3(a, b, c, i, j, k):
return (
a[i] * (b[j] * c[k] - b[k] * c[j])
- a[j] * (b[i] * c[k] - b[k] * c[i])
+ a[k] * (b[i] * c[j] - b[j] * c[i])
)
def verify(data):
points = read_points(data)
# A plane or sphere lifts to a hyperplane under (x,y,z) -> (x,y,z,x²+y²+z²).
# Conversely, a hyperplane equation in the lifted coordinates yields a plane
# or, by completing the square, a sphere through the original points.
lifted = [(x, y, z, x*x + y*y + z*z) for x, y, z in points]
for i, origin in enumerate(lifted[:-4]):
differences = [tuple(a - b for a, b in zip(p, origin)) for p in lifted[i+1:]]
for j, k, l in combinations(range(len(differences) - 1), 3):
a, b, c = differences[j], differences[k], differences[l]
# Cofactors give the normal to the three difference vectors in R^4.
w = det3(a, b, c, 1, 2, 3)
x = -det3(a, b, c, 0, 2, 3)
y = det3(a, b, c, 0, 1, 3)
z = -det3(a, b, c, 0, 1, 2)
for d, e, f, g in differences[l+1:]:
if w*d + x*e + y*f + z*g == 0:
raise ValueError("Five distinct points lie on a plane or a sphere.")
return len(points)
def main():
data = sys.stdin.buffer.read(MAX_BYTES + 1)
try:
if len(data) > MAX_BYTES:
raise ValueError("Submission exceeds the byte limit.")
if not data.isascii() or any(c < 32 and c not in (9, 10, 13) for c in data):
raise ValueError("Use plain ASCII text.")
score = verify(data)
except ValueError as error:
result = {"valid": False, "score": None, "message": str(error)}
else:
result = {"valid": True, "score": score, "message": "The construction is valid."}
print(json.dumps(result, separators=(",", ":")))
if __name__ == "__main__":
main()