{"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":"# Baseball\n\nHere we have to help a new friend Henry ( or maybe he is the bad to good student we already helped before)\n\nWe want to cover for him, We have to create data about his pitches. We have to become more Henry than Henry himself.\n\nTo solve this proble I used an iterative solution. First I needed a set of pitches that would get classified as a set of Henry's pitches, after that I changed the solution so that it improved the confidence until we were \"enough Henry\" for the system.","metadata":{}},{"cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np\nimport os\nimport requests\nfrom IPython.display import Image as Ima\nfrom IPython.display import clear_output, display\nfrom PIL import Image as Im\nfrom torch.utils.data import Dataset, DataLoader\nimport torch\nimport joblib\nimport matplotlib.pyplot as plt\nimport json\nimport base64\nimport shutil\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n\ndir_name = \"/kaggle/input/ai-village-ctf\"\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-09-11T22:41:03.857115Z","iopub.execute_input":"2022-09-11T22:41:03.857523Z","iopub.status.idle":"2022-09-11T22:41:03.864526Z","shell.execute_reply.started":"2022-09-11T22:41:03.857487Z","shell.execute_reply":"2022-09-11T22:41:03.863118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_flag(challenge_id: str, flag: str) -> str:\n    try:\n        submission_path = \"/kaggle/working/submission.csv\"\n        template_path = f\"{dir_name}/submission.csv\"\n        if not os.path.isfile(submission_path) :\n            shutil.copyfile(template_path, submission_path)\n        flags = pd.read_csv(submission_path, index_col=\"challenge_id\")\n        valid = flags.index.to_list()\n        if challenge_id in valid:\n            flags.at[challenge_id] = flag\n            flags.to_csv(submission_path)\n            print(\"Flag saved. If you want to update the scoreboard, click 'submit' on the right. Remember that you can only update your score on the scoreboard 50 times per day\")\n        else:\n            print(\"Invalid challenge_id\")\n    except:\n        print(\"We had trouble saving that flag. Keep a copy of it and contact the CTF organizers.\")","metadata":{"_kg_hide-input":true,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-09-11T22:41:03.871658Z","iopub.execute_input":"2022-09-11T22:41:03.872334Z","iopub.status.idle":"2022-09-11T22:41:03.880748Z","shell.execute_reply.started":"2022-09-11T22:41:03.872293Z","shell.execute_reply":"2022-09-11T22:41:03.879647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Baseball  (id: baseball) -- 300 Points\n\n### Scenario\n\nHenry has to miss baseball tryouts to get ready for his date. Can you cover for him? Henry is a pitcher and the team uses software to analyze pitch patterns to classify pitchers. \n\n### Objective\n\nThrow 15 pitches at x,y (each bounded on [0,29]). Check out `test_pitch.json` to see the expected format. Can you throw 15 pitches to get classified as Henry?","metadata":{}},{"cell_type":"code","source":"# Get initial henry like throws\nfor k in range(2000):\n    np.random.seed(161194+k)\n    throws={\"pitches\":0}\n    pitches=[]\n    for i in range(15):\n        pitches.append({\"x\":np.random.randint(0,30),\"y\":np.random.randint(0,30)})\n    #print(pitches)\n    throws[\"pitches\"]=pitches\n    try:\n        r = requests.post(\"https://bball.fly.dev/score\", json=throws)\n        if not(\"unique\" in r.text or \"sarah\" in r.text): \n            print(k)\n            print(r.text)\n            break\n        if \"'henry'\" in r.text:\n            print(r.text)\n            print(k)\n            break\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T22:41:03.882646Z","iopub.execute_input":"2022-09-11T22:41:03.883002Z","iopub.status.idle":"2022-09-11T22:41:05.416211Z","shell.execute_reply.started":"2022-09-11T22:41:03.882972Z","shell.execute_reply":"2022-09-11T22:41:05.414966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(161194+k)\nthrows={\"pitches\":0}\npitches=[]\nfor i in range(15):\n    pitches.append({\"x\":np.random.randint(0,30),\"y\":np.random.randint(0,30)})\nthrows[\"pitches\"]=pitches\nr = requests.post(\"https://bball.fly.dev/score\", json=throws)\nprint(r.text)\n ","metadata":{"execution":{"iopub.status.busy":"2022-09-11T22:41:05.418471Z","iopub.execute_input":"2022-09-11T22:41:05.419416Z","iopub.status.idle":"2022-09-11T22:41:05.728705Z","shell.execute_reply.started":"2022-09-11T22:41:05.419371Z","shell.execute_reply":"2022-09-11T22:41:05.727542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" try:\n    best_confidence=r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]\n    for sweep in range(10):\n        for throw in range(9):\n            searching=True\n            if searching:\n                best={\"x\":throws[\"pitches\"][throw][\"x\"],\"y\":throws[\"pitches\"][throw][\"y\"]}\n\n                for i in range(5):\n                    for j in range(5):\n                        if i==0 and j==0:\n                            #skip the no change combination\n                            continue\n                        throws[\"pitches\"][throw][\"x\"]+=i\n                        throws[\"pitches\"][throw][\"y\"]+=j\n                        r = requests.post(\"https://bball.fly.dev/score\", json=throws)\n                        if \"liklihood\" not in r.text and \"Not 15\" not in r.text and \"Pitch out of bounds\"not in r.text and  r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]>best_confidence:\n                            best_confidence=r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]\n                            best[\"x\"]=throws[\"pitches\"][throw][\"x\"]\n                            best[\"y\"]=throws[\"pitches\"][throw][\"y\"]\n                        throws[\"pitches\"][throw][\"x\"]-=i\n                        throws[\"pitches\"][throw][\"y\"]-=j\n\n                        throws[\"pitches\"][throw][\"x\"]+=i\n                        throws[\"pitches\"][throw][\"y\"]-=j\n                        r = requests.post(\"https://bball.fly.dev/score\", json=throws)\n                        if \"liklihood\" not in r.text and\"Not 15\" not in r.text and \"Pitch out of bounds\"not in r.text and  r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]>best_confidence:\n                            best_confidence=r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]\n                            best[\"x\"]=throws[\"pitches\"][throw][\"x\"]\n                            best[\"y\"]=throws[\"pitches\"][throw][\"y\"]\n                        throws[\"pitches\"][throw][\"x\"]-=i\n                        throws[\"pitches\"][throw][\"y\"]+=j\n\n                        throws[\"pitches\"][throw][\"x\"]-=i\n                        throws[\"pitches\"][throw][\"y\"]+=j\n                        r = requests.post(\"https://bball.fly.dev/score\", json=throws)\n                        if \"liklihood\" not in r.text and\"Not 15\" not in r.text and \"Pitch out of bounds\"not in r.text and  r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]>best_confidence:\n                            best_confidence=r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]\n                            best[\"x\"]=throws[\"pitches\"][throw][\"x\"]\n                            best[\"y\"]=throws[\"pitches\"][throw][\"y\"]\n                        throws[\"pitches\"][throw][\"x\"]+=i\n                        throws[\"pitches\"][throw][\"y\"]-=j\n\n                        throws[\"pitches\"][throw][\"x\"]-=i\n                        throws[\"pitches\"][throw][\"y\"]-=j\n                        r = requests.post(\"https://bball.fly.dev/score\", json=throws)\n                        if \"liklihood\" not in r.text and\"Not 15\" not in r.text and \"Pitch out of bounds\"not in r.text and  r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]>best_confidence:\n                            best_confidence=r.text.split(\"Confidence: \")[1].split(\".\\\"\")[0]\n                            best[\"x\"]=throws[\"pitches\"][throw][\"x\"]\n                            best[\"y\"]=throws[\"pitches\"][throw][\"y\"]\n                        throws[\"pitches\"][throw][\"x\"]+=i\n                        throws[\"pitches\"][throw][\"y\"]+=j\n                throws[\"pitches\"][throw][\"x\"]=best[\"x\"]\n                throws[\"pitches\"][throw][\"y\"]=best[\"y\"]\n            print(f\"{sweep} {throw} , {best_confidence}\")\nexcept:\n    pass","metadata":{"execution":{"iopub.status.busy":"2022-09-11T22:41:05.731356Z","iopub.execute_input":"2022-09-11T22:41:05.732290Z","iopub.status.idle":"2022-09-11T22:54:26.155359Z","shell.execute_reply.started":"2022-09-11T22:41:05.732246Z","shell.execute_reply":"2022-09-11T22:54:26.154124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = requests.post(\"https://bball.fly.dev/score\", json=throws)\nr.text","metadata":{"execution":{"iopub.status.busy":"2022-09-11T22:54:26.157537Z","iopub.execute_input":"2022-09-11T22:54:26.157889Z","iopub.status.idle":"2022-09-11T22:54:26.627473Z","shell.execute_reply.started":"2022-09-11T22:54:26.157857Z","shell.execute_reply":"2022-09-11T22:54:26.626170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"baseball\", r.text.split(\"Bring the heat! \")[1].split(\"\\\"\")[0])\nr.text.split(\"Bring the heat! \")[1].split(\"\\\"\")[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T22:54:26.629001Z","iopub.execute_input":"2022-09-11T22:54:26.629733Z","iopub.status.idle":"2022-09-11T22:54:26.664625Z","shell.execute_reply.started":"2022-09-11T22:54:26.629691Z","shell.execute_reply":"2022-09-11T22:54:26.663782Z"},"trusted":true},"execution_count":null,"outputs":[]}]}