{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"* Generate 15 unique pitches using suggest_int\n* If a prediction is henry, score is `confidence`, otherwise `-confidence`\n* Maximize the score until a flag is received!","metadata":{}},{"cell_type":"code","source":"import re\nimport requests\nimport optuna\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-12T03:31:37.941570Z","iopub.execute_input":"2022-09-12T03:31:37.942210Z","iopub.status.idle":"2022-09-12T03:31:37.948624Z","shell.execute_reply.started":"2022-09-12T03:31:37.942144Z","shell.execute_reply":"2022-09-12T03:31:37.947316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flag = None\n\ndef objective(trial):\n    global flag\n    data = {\"pitches\": []}\n    for i in range(15):\n        data[\"pitches\"].append({\"x\": trial.suggest_int(\"pitch-%02d-x\" % i, 0, 29), \"y\": trial.suggest_int(\"pitch-%02d-y\" % i, 0, 29)})\n    \n    # duplication check\n    pitchset = set()\n    for p in data[\"pitches\"]:\n        pitchset.add((p[\"x\"], p[\"y\"]))\n    trial.set_user_attr(\"constraint\", (15-len(pitchset),))\n    if len(pitchset) < 15:\n        raise optuna.exceptions.TrialPruned\n    \n    try:\n        r = requests.post(\"https://bball.fly.dev/score\", json=data)\n        print(trial.number, r.text)\n\n        maybe_flag = re.search(r\"\\S{128}\", r.text)\n        if maybe_flag:\n            # :tada:\n            flag = maybe_flag.group(0)\n            trial.study.stop()\n            return 1.0\n        if \"henry\" in r.text:\n            # looks like henry.\n            m = re.search(r\"(0.\\d+)\", r.text)\n            return float(m.group(1))\n        else:\n            # not henry.\n            m = re.search(r\"liklihood of ([\\d.]+)\", r.text)\n            return -float(m.group(1))\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")\n        raise\n\ndef constraints(trial):\n    return trial.user_attrs[\"constraint\"]\n        \noptuna.logging.set_verbosity(optuna.logging.WARNING)\nsampler = optuna.samplers.NSGAIISampler(constraints_func=constraints)\nstudy = optuna.create_study(direction=\"maximize\", sampler=sampler)\nstudy.optimize(objective, n_trials=2000, n_jobs=4)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-12T04:16:03.831545Z","iopub.execute_input":"2022-09-12T04:16:03.832053Z","iopub.status.idle":"2022-09-12T04:18:55.966907Z","shell.execute_reply.started":"2022-09-12T04:16:03.832014Z","shell.execute_reply":"2022-09-12T04:18:55.965478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"{len(study.trials)} trials.\")\nprint(flag)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T04:18:59.021563Z","iopub.execute_input":"2022-09-12T04:18:59.023055Z","iopub.status.idle":"2022-09-12T04:18:59.835967Z","shell.execute_reply.started":"2022-09-12T04:18:59.022994Z","shell.execute_reply":"2022-09-12T04:18:59.834803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}