{"files": {"main.py": "from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n", "policy.py": "POLICY = {\"adjacency_allowed\": False, \"blackout_day\": \"2026-11-02\", \"cancelled_blocks\": False, \"capacity\": 2, \"cutoff_hour\": 18, \"cutoff_inclusive\": False, \"duplicate_policy\": \"last\", \"duration_max\": 90, \"lead_minutes\": 60, \"priority_high_first\": False, \"report_order\": \"input\", \"timezone\": \"UTC\"}\n", "helpers.py": "from datetime import datetime, timedelta, timezone\n\ndef parse(s):\n    if s.endswith('Z'):\n        s = s[:-1] + '+00:00'\n    return datetime.fromisoformat(s)\n\ndef utc(dt):\n    return dt.astimezone(timezone.utc)\n", "core.py": "from datetime import datetime, timedelta, timezone\nfrom zoneinfo import ZoneInfo\nfrom helpers import parse, utc\n\n\ndef run(payload, policy):\n    now = utc(parse(payload['now']))\n    tz = ZoneInfo(policy['timezone'])\n    rows = payload.get('appointments', [])\n    # dedupe: keep first position, value per policy\n    order = []\n    chosen = {}\n    for i, a in enumerate(rows):\n        id_ = a['id']\n        if id_ not in chosen:\n            order.append(id_)\n            chosen[id_] = (i, a)\n        elif policy['duplicate_policy'] == 'last':\n            chosen[id_] = (i, a)\n    items = [chosen[k] for k in order]\n    hi = policy['priority_high_first']\n    items.sort(key=lambda t: (-t[1]['priority'] if hi else t[1]['priority'], t[0]))\n    accepted = []\n    selected, blocked, rejected = [], [], []\n    adj = policy['adjacency_allowed']\n    for idx, a in items:\n        ok = True\n        try:\n            d = a['duration']\n            if isinstance(d, bool) or not isinstance(d, int) or d <= 0 or d > policy['duration_max']:\n                ok = False\n            if ok:\n                st = utc(parse(a['start']))\n                en = st + timedelta(minutes=d)\n                if st < now + timedelta(minutes=policy['lead_minutes']):\n                    ok = False\n            if ok:\n                loc = st.astimezone(tz)\n                if loc.date().isoformat() == policy['blackout_day']:\n                    ok = False\n            if ok:\n                cut = datetime(loc.year, loc.month, loc.day, policy['cutoff_hour'], 0, tzinfo=tz)\n                cut = utc(cut)\n                if policy['cutoff_inclusive']:\n                    if not en <= cut:\n                        ok = False\n                elif not en < cut:\n                    ok = False\n            if ok and a['cancelled'] and not policy['cancelled_blocks']:\n                ok = False\n            if ok:\n                cnt = 0\n                for s2, e2 in accepted:\n                    if adj:\n                        ov = st < e2 and s2 < en\n                    else:\n                        ov = st <= e2 and s2 <= en\n                    if ov:\n                        cnt += 1\n                if cnt >= policy['capacity']:\n                    ok = False\n        except Exception:\n            ok = False\n        if not ok:\n            rejected.append((idx, a['id']))\n            continue\n        accepted.append((st, en))\n        if a['cancelled']:\n            blocked.append((idx, a['id']))\n        else:\n            selected.append((idx, a['id']))\n\n    def fmt(lst):\n        if policy['report_order'] == 'id':\n            lst = sorted(lst, key=lambda t: t[1])\n        else:\n            lst = sorted(lst, key=lambda t: t[0])\n        return [x[1] for x in lst]\n    return {'selected': fmt(selected), 'blocked': fmt(blocked), 'rejected': fmt(rejected)}\n"}}