{
  "files": {
    "main.py": "from core import run\nfrom policy import POLICY\n\n\ndef evaluate(payload):\n    return run(payload, POLICY)\n",
    "core.py": "from datetime import datetime, time, timedelta, timezone\nfrom zoneinfo import ZoneInfo\n\nfrom helpers import parse_utc, overlaps\n\n\ndef run(payload, policy):\n    now = parse_utc(payload['now'])\n    earliest = now + timedelta(minutes=policy['lead_minutes'])\n    zone = ZoneInfo(policy['timezone'])\n    retained = {}\n    for index, row in enumerate(payload['appointments']):\n        identifier = row['id']\n        if identifier not in retained or policy['duplicate_policy'] == 'last':\n            retained[identifier] = (index, row)\n\n    result = {'selected': [], 'blocked': [], 'rejected': []}\n    candidates = []\n    for identifier, (index, row) in retained.items():\n        try:\n            duration = row['duration']\n            priority = row['priority']\n            cancelled = row['cancelled']\n            if type(duration) is not int or not 0 < duration <= policy['duration_max']:\n                raise ValueError('Invalid duration')\n            if type(priority) is not int or type(cancelled) is not bool:\n                raise ValueError('Invalid appointment')\n            if cancelled and not policy['cancelled_blocks']:\n                raise ValueError('Cancelled')\n            start = parse_utc(row['start'])\n            end = start + timedelta(minutes=duration)\n            if start < earliest:\n                raise ValueError('Insufficient lead time')\n            local_start = start.astimezone(zone)\n            if local_start.date().isoformat() == policy['blackout_day']:\n                raise ValueError('Blackout day')\n            cutoff = datetime.combine(\n                local_start.date(), time(policy['cutoff_hour']), tzinfo=zone\n            ).astimezone(timezone.utc)\n            if end > cutoff or (end == cutoff and not policy['cutoff_inclusive']):\n                raise ValueError('After cutoff')\n            candidates.append((priority, index, identifier, start, end, cancelled))\n        except (ValueError, TypeError, OverflowError, KeyError):\n            result['rejected'].append(identifier)\n\n    candidates.sort(key=lambda item: (\n        -item[0] if policy['priority_high_first'] else item[0], item[1]\n    ))\n    accepted = []\n    for priority, index, identifier, start, end, cancelled in candidates:\n        count = sum(\n            overlaps(start, end, other_start, other_end, policy['adjacency_allowed'])\n            for other_start, other_end in accepted\n        )\n        if count >= policy['capacity']:\n            result['rejected'].append(identifier)\n        else:\n            accepted.append((start, end))\n            result['blocked' if cancelled else 'selected'].append(identifier)\n\n    for identifiers in result.values():\n        if policy['report_order'] == 'id':\n            identifiers.sort()\n        else:\n            identifiers.sort(key=lambda identifier: retained[identifier][0])\n    return result\n",
    "policy.py": "POLICY = {\n    'adjacency_allowed': True,\n    'blackout_day': '2026-11-01',\n    'cancelled_blocks': True,\n    'capacity': 1,\n    'cutoff_hour': 17,\n    'cutoff_inclusive': True,\n    'duplicate_policy': 'first',\n    'duration_max': 120,\n    'lead_minutes': 30,\n    'priority_high_first': True,\n    'report_order': 'id',\n    'timezone': 'America/New_York',\n}\n",
    "helpers.py": "from datetime import datetime, timezone\n\n\ndef parse_utc(value):\n    if not isinstance(value, str):\n        raise ValueError('Datetime must be a string')\n    if value.endswith('Z'):\n        value = value[:-1] + '+00:00'\n    parsed = datetime.fromisoformat(value)\n    if parsed.tzinfo is None or parsed.utcoffset() is None:\n        raise ValueError('Datetime must include an offset')\n    return parsed.astimezone(timezone.utc)\n\n\ndef overlaps(start, end, other_start, other_end, adjacency_allowed):\n    if adjacency_allowed:\n        return start < other_end and other_start < end\n    return start <= other_end and other_start <= end\n"
  }
}