Read the checking program (59 lines of Python)
"""Validate a mixed-size group schedule and count its rounds."""
from itertools import combinations
import json
from math import comb
from pathlib import Path
import sys
MAX_BYTES = 8192
def score(rounds, participants, fours, fives):
limit = comb(participants, 2) // (6 * fours + 10 * fives)
if not isinstance(rounds, list) or not 1 <= len(rounds) <= limit:
raise ValueError(f"Submit between 1 and {limit} rounds.")
people = set(range(participants))
seen_pairs = set()
for groups in rounds:
if not isinstance(groups, list) or len(groups) != fours + fives:
raise ValueError("Use the specified number of groups in every round.")
if any(not isinstance(group, list) or len(group) not in (4, 5) for group in groups):
raise ValueError("Every group must have four or five participants.")
if sum(len(group) == 4 for group in groups) != fours:
raise ValueError("Use the specified number of four-person groups in every round.")
members = [person for group in groups for person in group]
if any(type(person) is not int for person in members) or set(members) != people:
raise ValueError("Include each participant exactly once per round using integer IDs.")
for group in groups:
for pair in combinations(sorted(group), 2):
if pair in seen_pairs:
raise ValueError("A pair of participants shares a group more than once.")
seen_pairs.add(pair)
return len(rounds)
def main():
instance = json.loads(Path(__file__).with_name("instance.json").read_text())
data = sys.stdin.buffer.read(MAX_BYTES + 1)
try:
if len(data) > MAX_BYTES:
raise ValueError("Submission exceeds the byte limit.")
try:
rounds = json.loads(data.decode("ascii"))
except (ValueError, RecursionError):
raise ValueError("Submit a JSON array of rounds using ASCII.") from None
value = score(rounds, instance["participants"], instance["fours"], instance["fives"])
result = {"valid": True, "score": value, "message": "The schedule is valid."}
except ValueError as error:
result = {"valid": False, "score": None, "message": str(error)}
print(json.dumps(result))
if __name__ == "__main__":
main()