2016 challenge 4
[cipher-tools.git] / bombe.ipynb
index 59442e4f4da5bf6481be0f923171939245d2c938..93e0ccbfcd5ac4996251c5eb08db7e6deadd969b 100644 (file)
@@ -75,7 +75,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 31,
    "metadata": {
     "collapsed": false
    },
     "        self.connections += [Connection([bank_before, bank_after], scrambler)]\n",
     "        \n",
     "    def read_menu(self, menu):\n",
+    "        self.connections = []\n",
     "        for item in menu:\n",
     "            scrambler = Scrambler(self.wheel1_spec, self.wheel2_spec, self.wheel3_spec,\n",
     "                                  self.reflector_spec,\n",
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 32,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 33,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 34,
    "metadata": {
     "collapsed": false
    },
        "'opgndxcrwomnlnecjz'"
       ]
      },
-     "execution_count": 7,
+     "execution_count": 34,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 35,
    "metadata": {
     "collapsed": false
    },
        "'aas'"
       ]
      },
-     "execution_count": 8,
+     "execution_count": 35,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 36,
    "metadata": {
     "collapsed": false
    },
        " MenuIem(before='e', after='z', number=18)]"
       ]
      },
-     "execution_count": 9,
+     "execution_count": 36,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
+   "execution_count": 37,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 11,
+   "execution_count": 38,
    "metadata": {
     "collapsed": false
    },
        " MenuIem(before='e', after='z', number=18)]"
       ]
      },
-     "execution_count": 11,
+     "execution_count": 38,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 12,
+   "execution_count": 39,
    "metadata": {
     "collapsed": false
    },
        "'s'"
       ]
      },
-     "execution_count": 12,
+     "execution_count": 39,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": 42,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 43,
    "metadata": {
     "collapsed": false
    },
        "18"
       ]
      },
-     "execution_count": 14,
+     "execution_count": 43,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 27,
    "metadata": {
     "collapsed": false
    },
      "name": "stdout",
      "output_type": "stream",
      "text": [
+      "['t', 'o'] aaa\n",
+      "['h', 'p'] aab\n",
+      "['i', 'g'] aac\n",
+      "['s', 'n'] aad\n",
+      "['i', 'd'] aae\n",
+      "['s', 'x'] aaf\n",
+      "['a', 'c'] aag\n",
+      "['t', 'r'] aah\n",
+      "['e', 'w'] aai\n",
+      "['s', 'o'] aaj\n",
+      "['t', 'm'] aak\n",
+      "['m', 'n'] aal\n",
+      "['e', 'l'] aam\n",
+      "['s', 'n'] aan\n",
+      "['s', 'e'] aao\n",
+      "['a', 'c'] aap\n",
+      "['g', 'j'] aaq\n",
+      "['e', 'z'] aar\n",
       "['t', 'o'] aaa\n",
       "['h', 'p'] aab\n",
       "['i', 'g'] aac\n",
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": 44,
    "metadata": {
     "collapsed": false
    },
     {
      "data": {
       "text/plain": [
-       "False"
+       "'ot:hp:gi:ns:di:sx:ac:rt:ew:os:mt:mn:el:ns:es:ac:gj:ez'"
       ]
      },
-     "execution_count": 16,
+     "execution_count": 44,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "':'.join(cat(sorted(c.banks)) for c in bombe.connections)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 45,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'aaa:aab:aac:aad:aae:aaf:aag:aah:aai:aaj:aak:aal:aam:aan:aao:aap:aaq:aar'"
+      ]
+     },
+     "execution_count": 45,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "':'.join(cat(c.scrambler.wheel_positions_l) for c in bombe.connections)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 56,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "True"
+      ]
+     },
+     "execution_count": 56,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 17,
+   "execution_count": 57,
    "metadata": {
     "collapsed": false
    },
     {
      "data": {
       "text/plain": [
-       "{'a': True,\n",
-       " 'b': True,\n",
-       " 'c': True,\n",
-       " 'd': True,\n",
-       " 'e': True,\n",
-       " 'f': True,\n",
-       " 'g': True,\n",
-       " 'h': True,\n",
-       " 'i': True,\n",
-       " 'j': True,\n",
-       " 'k': True,\n",
-       " 'l': True,\n",
-       " 'm': True,\n",
-       " 'n': True,\n",
-       " 'o': True,\n",
-       " 'p': True,\n",
-       " 'q': True,\n",
-       " 'r': True,\n",
-       " 's': True,\n",
+       "{'a': False,\n",
+       " 'b': False,\n",
+       " 'c': False,\n",
+       " 'd': False,\n",
+       " 'e': False,\n",
+       " 'f': False,\n",
+       " 'g': False,\n",
+       " 'h': False,\n",
+       " 'i': False,\n",
+       " 'j': False,\n",
+       " 'k': False,\n",
+       " 'l': False,\n",
+       " 'm': False,\n",
+       " 'n': False,\n",
+       " 'o': False,\n",
+       " 'p': False,\n",
+       " 'q': False,\n",
+       " 'r': False,\n",
+       " 's': False,\n",
        " 't': True,\n",
-       " 'u': True,\n",
-       " 'v': True,\n",
-       " 'w': True,\n",
-       " 'x': True,\n",
-       " 'y': True,\n",
-       " 'z': True}"
+       " 'u': False,\n",
+       " 'v': False,\n",
+       " 'w': False,\n",
+       " 'x': False,\n",
+       " 'y': False,\n",
+       " 'z': False}"
       ]
      },
-     "execution_count": 17,
+     "execution_count": 57,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 18,
+   "execution_count": 48,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 19,
+   "execution_count": 49,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "a : abcdefghijklmnopqrstuvwxyz\n",
+      "b : a.cde.ghij.lmnop.rst..wx.z\n",
+      "c : abcdefghijklmnopqrstuvwxyz\n",
+      "d : abcdefghijklmnopqrstuvwx.z\n",
+      "e : abcdefghijklmnopqrstuvwxyz\n",
+      "f : a.cde.ghij.lmnop.rst..wx.z\n",
+      "g : abcdefghijklmnopqrst.vwxyz\n",
+      "h : abcdefghijklmnopqrstuvwxyz\n",
+      "i : abcdefghijklmnopqrstu.wxyz\n",
+      "j : abcdefghi.klmnopqrstuvwxyz\n",
+      "k : a.cde.ghij.lmnop.rst..wx.z\n",
+      "l : abcdefghijklmnopqrstuvwxyz\n",
+      "m : abcdefghijklmnopqrstuvwxyz\n",
+      "n : abcdefghijklmnopqrstuvwxyz\n",
+      "o : abcdefghijklmnopqrstuvwxyz\n",
+      "p : abcdefghijklmnopqrstuvwxyz\n",
+      "q : a.cde.ghij.lmnop.rst..wx.z\n",
+      "r : abcdefghijklmnopqrstuvwxyz\n",
+      "s : abcdefghijklmnopqrstuvwxyz\n",
+      "t : abcdefghijklmnopqrstuvwxyz\n",
+      "u : a.cde..hij.lmnop.rst..wx.z\n",
+      "v : a.cde.gh.j.lmnop.rst..wx.z\n",
+      "w : abcdefghijklmnopqrstuvwxyz\n",
+      "x : abcdefghijklmnopqrstuvwxyz\n",
+      "y : a.c.e.ghij.lmnop.rst..wx.z\n",
+      "z : abcdefghijklmnopqrstuvwxyz\n"
+     ]
+    }
+   ],
+   "source": [
+    "for b in sorted(bombe.banks):\n",
+    "    print(b, ': ', end='')\n",
+    "    for w in sorted(bombe.banks[b]):\n",
+    "        if bombe.banks[b][w]:\n",
+    "            print(w, end='')\n",
+    "        else:\n",
+    "            print('.', end='')\n",
+    "    print('')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 50,
    "metadata": {
     "collapsed": false
    },
        "('a', 'a', 'a')"
       ]
      },
-     "execution_count": 19,
+     "execution_count": 50,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 20,
+   "execution_count": 51,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 21,
+   "execution_count": 52,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 22,
+   "execution_count": 53,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 23,
+   "execution_count": 54,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 24,
+   "execution_count": 55,
    "metadata": {
     "collapsed": false
    },
        "1"
       ]
      },
-     "execution_count": 24,
+     "execution_count": 55,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 37,
+   "execution_count": 58,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 38,
+   "execution_count": 59,
    "metadata": {
     "collapsed": false
    },
        "('e', 'l', 'e')"
       ]
      },
-     "execution_count": 38,
+     "execution_count": 59,
      "metadata": {},
      "output_type": "execute_result"
     }
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.4.3+"
+   "version": "3.5.1+"
   }
  },
  "nbformat": 4,