Read the checking program (50 lines of Python)
"""Score a Boolean function's truth table by its distance from every affine function."""
import json
import sys
VARIABLES = 13
SIZE = 1 << VARIABLES
MAX_BYTES = 2 * SIZE
def nonlinearity(bits):
"""Return the least number of table entries that differ from an affine function."""
size = len(bits)
walsh = [1 - 2 * b for b in bits]
stride = 1
while stride < size:
for start in range(0, size, 2 * stride):
for i in range(start, start + stride):
a, b = walsh[i], walsh[i + stride]
walsh[i], walsh[i + stride] = a + b, a - b
stride *= 2
return (size - max(map(abs, walsh))) // 2
def score(data):
text = "".join(data.split())
if set(text) - {"0", "1"}:
raise ValueError("Use only the characters 0 and 1, with optional spaces and line breaks.")
if len(text) != SIZE:
raise ValueError(f"Submit exactly {SIZE} table entries; found {len(text)}.")
return nonlinearity([int(c) for c in text])
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.")
value = score(data.decode("ascii"))
result = {"valid": True, "score": value, "message": f"The table is {value} entries from the nearest affine function."}
except ValueError as error:
result = {"valid": False, "score": None, "message": str(error)}
print(json.dumps(result))
if __name__ == "__main__":
main()