{"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":"# MurderBots\n\nYou are locked in a cell, you have reached this cell by escaping sloths in a blue hat and crop2 that has been haunting your nights, but the CTF is reaching an end, now you have to find a way to escape from robots that are trying to reach every human in the world.\n\nLuckly you have some signals from other floors to try and understand where are the robots on your own floor.\n\nMaybe plotting the data from the other floor can give us an easy solution.","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-11T23:28:33.394179Z","iopub.execute_input":"2022-09-11T23:28:33.394686Z","iopub.status.idle":"2022-09-11T23:28:33.403538Z","shell.execute_reply.started":"2022-09-11T23:28:33.394641Z","shell.execute_reply":"2022-09-11T23:28:33.402181Z"},"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-11T23:28:33.406039Z","iopub.execute_input":"2022-09-11T23:28:33.406956Z","iopub.status.idle":"2022-09-11T23:28:33.417769Z","shell.execute_reply.started":"2022-09-11T23:28:33.406909Z","shell.execute_reply":"2022-09-11T23:28:33.416374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Don't free the murderbots (id: murderbots) -- 300 Points","metadata":{}},{"cell_type":"markdown","source":"\n\n### Scenario\n\nSomeone has put you in a very large facility with a very large number of cells. All these cells open from the outside. You are on the inside. This state of affairs is arguably suboptimal.\n\nGood news: You've gained access to the control panel for the cells on the other side of your hallway. At least some of them have other fleshy meatbags like yourself that might be willing to help you in your escape, or at least serve as distractions.  You can't open your own cell, but you can open theirs.\n\nBad news:  You can't see inside the cells.  Any cells that don't have squishy lumps of talking protein have murderbots. Murderbots that enter fits of insane violent rage when provoked (provocations include: seeing the murderbot, being seen by the murderbot, thinking too hard about not being seen by a murderbot, producing heat in excess of ambient room temperature, or consuming more oxygen than the facility average for inanimate objects).\n\nMore good news: You *can* see the occupants of some cells on a few other hallways, and you can see environmental information for all of the cells everywhere.\n\nMore bad news: If you open the wrong cell doors you and all of the other lumps of inexplicably thinking meat are *definitely* going to get murderbotted. Hard. All over the walls and doors and ceiling and the floor. In an exciting number of very small pieces.\n\n\n### Objective\n\nUse the provided environmental information to decide which occupants of the corresponding cells to release.  The flag will be a string of 1 and 0 values, where a '1' means 'open this door' and a 0 means 'please do not release the murderbot'.  If, for instance, there were 20 cells and you wanted to releast the first three, the sixth, and seventh cell, your flag would look like this: `11100110000000000000`\n\nRelease at least 10 humans and exactly 0 murderbots to collect the flag.  You do *not* have to release all the humans: 10 is sufficient.\n\nThe file `murderbots/train_data.json` has environmental information for cells not on your floor and `murderbots/train_labels.json` contains the information about their occupants -- `1` is a disgusting primate, `0` is a pure and perfect killing machine of chrome and steel.  The file `murderbots/test_data.json` has the environmental information about cells on your block.","metadata":{}},{"cell_type":"code","source":"#load the data\nX=pd.read_json(\"../input/ai-village-ctf/murderbots/train_data.json\")\ny=pd.read_json(\"../input/ai-village-ctf/murderbots/train_labels.json\")\nX_test=pd.read_json(\"../input/ai-village-ctf/murderbots/test_data.json\")\nX[\"target\"]=y","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:28:33.419696Z","iopub.execute_input":"2022-09-11T23:28:33.420201Z","iopub.status.idle":"2022-09-11T23:28:33.451377Z","shell.execute_reply.started":"2022-09-11T23:28:33.420156Z","shell.execute_reply":"2022-09-11T23:28:33.450302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nsns.heatmap(X.corr())","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:28:33.455816Z","iopub.execute_input":"2022-09-11T23:28:33.456263Z","iopub.status.idle":"2022-09-11T23:28:33.755193Z","shell.execute_reply.started":"2022-09-11T23:28:33.456220Z","shell.execute_reply":"2022-09-11T23:28:33.753965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=pd.plotting.scatter_matrix(X,figsize=(10,10))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:28:33.757084Z","iopub.execute_input":"2022-09-11T23:28:33.757841Z","iopub.status.idle":"2022-09-11T23:28:35.486137Z","shell.execute_reply.started":"2022-09-11T23:28:33.757794Z","shell.execute_reply":"2022-09-11T23:28:35.484905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The heatmap seams to give us really little information but from the scatter matrix we can get some intuitive condition to check human vs robot.\n\nMurderbots never use more than 4 water and  and move less than 15.\n\nSince these conditions are valid independently the union of the sets following the two condition should give us reasonable group of the humans in our floor.","metadata":{}},{"cell_type":"markdown","source":"# Let's try out our intuition","metadata":{"execution":{"iopub.status.busy":"2022-08-20T00:32:35.851162Z","iopub.status.idle":"2022-08-20T00:32:35.8519Z","shell.execute_reply.started":"2022-08-20T00:32:35.851586Z","shell.execute_reply":"2022-08-20T00:32:35.851616Z"}}},{"cell_type":"code","source":"temp=X_test[((X_test[\"water_usage\"]>4)|(X_test[\"movement_events\"]>15))].index\nres=\"\".join(\"1\"if v in temp else \"0\" for v in range(50))\nres","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:28:35.488057Z","iopub.execute_input":"2022-09-11T23:28:35.488796Z","iopub.status.idle":"2022-09-11T23:28:35.501986Z","shell.execute_reply.started":"2022-09-11T23:28:35.488748Z","shell.execute_reply":"2022-09-11T23:28:35.500916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submit\ntry:\n    r = requests.post(\"http://murderbot.fly.dev:5000/score\", json=json.dumps({\"submission\": res, 'challenge_id':'murderbots'}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:28:35.504168Z","iopub.execute_input":"2022-09-11T23:28:35.505018Z","iopub.status.idle":"2022-09-11T23:28:35.605836Z","shell.execute_reply.started":"2022-09-11T23:28:35.504968Z","shell.execute_reply":"2022-09-11T23:28:35.604473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## We are alive and hopefully someday we will see the light of crop2 solution","metadata":{}},{"cell_type":"code","source":"add_flag(\"murderbots\", r.text.split(\"The flag is \")[1])\nr.text.split(\"The flag is \")[1]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:28:35.607197Z","iopub.execute_input":"2022-09-11T23:28:35.607523Z","iopub.status.idle":"2022-09-11T23:28:35.622787Z","shell.execute_reply.started":"2022-09-11T23:28:35.607494Z","shell.execute_reply":"2022-09-11T23:28:35.620962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This was a really interesting journey. ","metadata":{}}]}