Read the checking program (55 lines of Python)
"""Check distinct ternary points for an affine line and score their cardinality."""
import json
import sys
from itertools import combinations
MAX_BYTES = 65610
DIMENSION = 8
MAX_POINTS = 6561
def verify(data):
lines = data.splitlines()
if not 1 <= len(lines) <= MAX_POINTS:
raise ValueError("The submission has the wrong number of points.")
points = []
coordinate_mask = (1 << DIMENSION) - 1
for line in lines:
if len(line) != DIMENSION:
raise ValueError("Each point must have exactly the specified number of digits.")
if any(c not in b"012" for c in line):
raise ValueError("Each coordinate must be 0, 1, or 2.")
ones = sum(1 << i for i, c in enumerate(line) if c == ord("1"))
twos = sum(1 << i for i, c in enumerate(line) if c == ord("2"))
points.append((coordinate_mask ^ (ones | twos), ones, twos))
by_digits = {(ones, twos) for _, ones, twos in points}
if len(by_digits) != len(points):
raise ValueError("The same point occurs more than once.")
# For each distinct x and y, the only possible third point is -x-y.
# Masks compute its coordinates in parallel using the F_3 addition table.
for (x0, x1, x2), (y0, y1, y2) in combinations(points, 2):
ones = (x0 & y2) | (x2 & y0) | (x1 & y1)
twos = (x0 & y1) | (x1 & y0) | (x2 & y2)
if (ones, twos) in by_digits:
raise ValueError("Three distinct points form an affine line.")
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()