Read the checking program (52 lines of Python)
"""Check the weight, distinctness, and minimum Hamming distance of binary words."""
import json
import sys
from itertools import combinations
MAX_BYTES = 42300
LENGTH = 34
DISTANCE = 8
WEIGHT = 7
MAX_WORDS = 1175
def verify(data):
lines = data.splitlines()
if not 1 <= len(lines) <= MAX_WORDS:
raise ValueError("The submission has the wrong number of words.")
words = []
for line in lines:
if len(line) != LENGTH:
raise ValueError("Every word must have exactly the specified length.")
if any(c not in b"01" for c in line):
raise ValueError("A codeword must contain only 0 and 1.")
word = int(line, 2)
if word.bit_count() != WEIGHT:
raise ValueError("A word has the wrong Hamming weight.")
words.append(word)
if len(set(words)) != len(words):
raise ValueError("The same word occurs more than once.")
# XOR is nonzero exactly in the positions where the words differ.
for left, right in combinations(words, 2):
if (left ^ right).bit_count() < DISTANCE:
raise ValueError("Two words are closer than the minimum Hamming distance.")
return len(words)
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()