Last active
May 7, 2022 05:25
-
-
Save Karthik-d-k/519950a9e256451a3e9a59e43837bb4c to your computer and use it in GitHub Desktop.
Solution for the puzzle posted by 3Blue1Brown on twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Solution for the puzzle posted by [3Blue1Brown on twitter](https://twitter.com/3blue1brown/status/1522749515207479302?s=20&t=BehsaJfKLV7_TBkHYwH-8Q)....." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CLOSE = π€\n", | |
"OPEN = π€\n" | |
] | |
} | |
], | |
"source": [ | |
"CLOSE = \"\\U0001F5A4\"\n", | |
"OPEN = \"\\U0001F90D\"\n", | |
"print(f\"CLOSE = {CLOSE}\\nOPEN = {OPEN}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class LOCKERS():\n", | |
" def __init__(self, N, Close, Open, all_close_at_first=True):\n", | |
" self.N = N\n", | |
" self.Close = Close\n", | |
" self.Open = Open\n", | |
" self.all_close_at_first = all_close_at_first\n", | |
" self.states = []\n", | |
" \n", | |
" def __repr__(self):\n", | |
" return f\"{self.states}\"\n", | |
" \n", | |
" def initialize(self):\n", | |
" \"\"\"\n", | |
" Activate/Initialize puzzle to given state depending on flag `all_close_at_first`\n", | |
" \"\"\"\n", | |
" if self.all_close_at_first:\n", | |
" self.states = list(self.Close * self.N)\n", | |
" else:\n", | |
" self.states = list(self.Open * self.N)\n", | |
" \n", | |
" def toggle(self, state):\n", | |
" \"\"\"\n", | |
" toggle to opposite state w.r.t. given `state`\n", | |
" \"\"\"\n", | |
" if (state == self.Close):\n", | |
" return self.Open\n", | |
" if (state == self.Open):\n", | |
" return self.Close\n", | |
" \n", | |
" def solve(self):\n", | |
" \"\"\"\n", | |
" Solve the puzzle with initial state set\n", | |
" \"\"\"\n", | |
" for num in range(self.N):\n", | |
" for state_idx in range(self.N):\n", | |
" if ((state_idx + 1) % (num + 1) == 0):\n", | |
" self.states[state_idx] = self.toggle(self.states[state_idx])\n", | |
" print(self)\n", | |
" \n", | |
" def final_ans(self):\n", | |
" \"\"\"\n", | |
" print final answer to stdout\n", | |
" \"\"\"\n", | |
" open_idxs = []\n", | |
" close_idxs = []\n", | |
" for i, o in enumerate(self.states):\n", | |
" if (o == self.Close):\n", | |
" close_idxs.append(i+1)\n", | |
" if (o == self.Open):\n", | |
" open_idxs.append(i+1)\n", | |
" \n", | |
" print(self)\n", | |
" print()\n", | |
" print(f\"OPEN LOCKS = {open_idxs}\")\n", | |
" print()\n", | |
" print(f\"CLOSE LOCKS = {close_idxs}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lockers = LOCKERS(N = 10, Close = CLOSE, Open = OPEN, all_close_at_first=True)\n", | |
"lockers.initialize()\n", | |
"lockers" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n" | |
] | |
} | |
], | |
"source": [ | |
"lockers.solve()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"\n", | |
"OPEN LOCKS = [1, 4, 9]\n", | |
"\n", | |
"CLOSE LOCKS = [2, 3, 5, 6, 7, 8, 10]\n" | |
] | |
} | |
], | |
"source": [ | |
"lockers.final_ans()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lockers = LOCKERS(N = 10, Close = CLOSE, Open = OPEN, all_close_at_first=False)\n", | |
"lockers.initialize()\n", | |
"lockers" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n" | |
] | |
} | |
], | |
"source": [ | |
"lockers.solve()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€', 'π€']\n", | |
"\n", | |
"OPEN LOCKS = [2, 3, 5, 6, 7, 8, 10]\n", | |
"\n", | |
"CLOSE LOCKS = [1, 4, 9]\n" | |
] | |
} | |
], | |
"source": [ | |
"lockers.final_ans()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment