Read the checking program (77 lines of Python)
"""Check pairwise angles of nonzero directions are at least 60 degrees, exactly in Q(sqrt(2))."""
import json
import sys
import re
from itertools import combinations
MAX_BYTES = 450000
DIMENSION = 11
MAX_POINTS = 867
def positive(a, b):
"""Test a + b sqrt(2) > 0 using integers, including at equality."""
if a >= 0 and b >= 0:
return a > 0 or b > 0
if a <= 0 and b <= 0:
return False
return a*a > 2*b*b if a > 0 else 2*b*b > a*a
def dot(left, right):
a = b = 0
for (x, y), (u, v) in zip(left, right):
a += x*u + 2*y*v
b += x*v + y*u
return a, b
def verify(data):
lines = data.splitlines()
if not 1 <= len(lines) <= MAX_POINTS:
raise ValueError("The submission has the wrong number of directions.")
directions = []
for line in lines:
tokens = line.split()
if len(tokens) != DIMENSION:
raise ValueError("Each direction must have exactly the specified number of coordinates.")
point = []
for token in tokens:
if re.fullmatch(rb"-?[0-9]{1,18}(:-?[0-9]{1,18})?", token) is None:
raise ValueError("A coordinate must be an integer or a pair of integers written a:b.")
real, separator, radical = token.partition(b":")
point.append((int(real), int(radical) if separator else 0))
norm = dot(point, point)
if norm == (0, 0):
raise ValueError("A direction vector is zero.")
directions.append((point, norm))
# For a positive dot product, cos(theta) <= 1/2 iff 4 dot² <= |p|²|q|².
# Nonpositive dot products pass automatically. Identical rays fail.
for (p, (pp, pr)), (q, (qq, qr)) in combinations(directions, 2):
a, b = dot(p, q)
if positive(a, b) and positive(
4 * (a*a + 2*b*b) - (pp*qq + 2*pr*qr),
8*a*b - (pp*qr + pr*qq),
):
raise ValueError("Two directions form an angle smaller than 60 degrees.")
return len(directions)
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()