Updated notebooks for new library organisation
[cipher-tools.git] / 2017 / 2017-challenge5.ipynb
index 214034c35df882d3d6b95cf25dfbd858af41537c..609d0179f5dfaeae78719a2e2e632a4cd50ad142 100644 (file)
     "import matplotlib.pyplot as plt\n",
     "%matplotlib inline\n",
     "\n",
-    "from cipherbreak import *\n",
-    "from text_prettify import *\n",
+    "from cipher.caesar import *\n",
+    "from cipher.affine import *\n",
+    "from cipher.keyword_cipher import *\n",
+    "from cipher.vigenere import *\n",
+    "from support.utilities import *\n",
+    "from support.text_prettify import *\n",
+    "from support.language_models import *\n",
     "\n",
     "ca = open('5a.ciphertext').read()\n",
     "cb = open('5b.ciphertext').read()"
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {},
-   "outputs": [],
-   "source": [
-    "# def tpack(text, width=100):\n",
-    "#     lines = [text[0]]\n",
-    "#     for word in text[1:]:\n",
-    "#         if len(lines[-1]) + 1 + len(word) <= width:\n",
-    "#             lines[-1] += (' ' + word)\n",
-    "#         else:\n",
-    "#             lines += [word]\n",
-    "#     return lines"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 3,
-   "metadata": {},
    "outputs": [
     {
      "name": "stdout",
@@ -56,7 +45,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 3,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -84,7 +73,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [
     {
@@ -93,7 +82,7 @@
        "(24, -1733.3180566179738)"
       ]
      },
-     "execution_count": 5,
+     "execution_count": 4,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 5,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 6,
    "metadata": {},
    "outputs": [
     {
        "((17, 21, True), -1723.2877779777489)"
       ]
      },
-     "execution_count": 7,
+     "execution_count": 6,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 7,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 50,
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(('decoy', <KeywordWrapAlphabet.from_last: 2>), -1505.1870286708981)"
+      ]
+     },
+     "execution_count": 9,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "ka, score = keyword_break_mp(sca_text)\n",
+    "ka, score"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
    "metadata": {},
    "outputs": [
     {
        "('decoyzabfghijklmnpqrstuvwx', -4117.559010215594)"
       ]
      },
-     "execution_count": 50,
+     "execution_count": 10,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 51,
+   "execution_count": 11,
    "metadata": {},
    "outputs": [
     {
    ],
    "source": [
     "cipher_translation = ''.maketrans(cipher_alphabet, string.ascii_lowercase)\n",
-    "print(lcat(tpack(segment(sanitise(ca).translate(cipher_translation)))))"
+    "print(prettify(sanitise(ca).translate(cipher_translation)))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": 12,
    "metadata": {},
    "outputs": [
     {
        "'etoainhsrdlumwycfgpbvkxjqz'"
       ]
      },
-     "execution_count": 16,
+     "execution_count": 12,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 17,
+   "execution_count": 13,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "'yrkfdlbqpoisjcumzeathwxgvn'"
+       "'yrkfdlbqpoisjcumzaewthgxnv'"
       ]
      },
-     "execution_count": 17,
+     "execution_count": 13,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 18,
+   "execution_count": 14,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "{'A': 'p',\n",
+       "{'A': 'g',\n",
        " 'B': 'h',\n",
        " 'C': 'w',\n",
        " 'D': 'i',\n",
-       " 'E': 'g',\n",
+       " 'E': 'p',\n",
        " 'F': 'a',\n",
-       " 'G': 'j',\n",
-       " 'H': 'v',\n",
+       " 'G': 'x',\n",
+       " 'H': 'k',\n",
        " 'I': 'l',\n",
        " 'J': 'm',\n",
        " 'K': 'o',\n",
        " 'L': 'n',\n",
        " 'M': 'c',\n",
-       " 'N': 'z',\n",
+       " 'N': 'q',\n",
        " 'O': 'd',\n",
        " 'P': 'r',\n",
        " 'Q': 's',\n",
        " 'R': 't',\n",
        " 'S': 'u',\n",
-       " 'T': 'b',\n",
+       " 'T': 'v',\n",
        " 'U': 'y',\n",
-       " 'V': 'q',\n",
-       " 'W': 'k',\n",
-       " 'X': 'x',\n",
+       " 'V': 'z',\n",
+       " 'W': 'b',\n",
+       " 'X': 'j',\n",
        " 'Y': 'e',\n",
        " 'Z': 'f'}"
       ]
      },
-     "execution_count": 18,
+     "execution_count": 14,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 19,
+   "execution_count": 15,
    "metadata": {},
    "outputs": [
     {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "mirki mjnda ehisp noeai rrabe daose lwuvi sclio oedgu tshed adotm iveth ereod exbnu sthel nwils titan othao vshem ikhib egeeo vadoi ccedg utthe rehis geeoo nrios nmdem iodio dther eason thaop muwht npnno herrn nmhid geeor iosiw vedio dshel efton messi pegut snmet haopt ellsm eshea snvio dyesh nuldm nbeno athao vshem ikhib eclio oedth asmei oyhal eahib ethed nwume otjnd aeyio tedfr nmlno dnoad notvo nyhny shevo eygut thefa fthwh icter yisha ddeoa oiwib atkao istno elaot elaos nmere miaos aothe eches usrnn mitth egrat ashmu seuma tyise owrkc tedus aopio nther cnlki lchig etaww acher aobeo tedao theoa oetee othwe oturk aisve dthem useum tntes tatio dthed nwume otasi utheo tawsn apues sthea mcera ilwac hersw hnnlr eillk hidmn bedno cerhi csash nuldo thibe geeos urcra sedif terth edasw nberk itgnd rumgu tatle ibesm eynod eraop yhite lseth ekdeb elnce dnoen ddfei turey isifr aexer uooao prnuo dthre esade snfth elaot elwno sasta opnfi saopl ernyn fgliw viody hatet alest hitth emuse umtha ovyer eidde daoth ernmi ocera ndthe kreid aosez ueowe yyggy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg gyyyg yygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygyyg yyygy gggyg ggygg gyggg ygggy gggyg ggygg gyggg ygggy gggyg ggygg gyggg ygggy gggyg ggygg gyggg ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yygyy ygyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy ygyyy gyyyy gyyyg ggyyy yyggg yyygy yygyy ygyyy gyyyy gggyy gyyyg ygggy yyggg gyygg gyyyg ggyyg ggygy yygyy yygyg yygyy gyyyg yyyyy gygyy ggyyg yygyg yyyyg yyygg yggyg yygyy gyyyy ygyyg yyygy yyygy yygyy ygyyg yyygy gyyyg ygyyy yygyy ygygy gygyg yyygy yygyy ygygy gygyy gyygy yyyyg yyygy ygyyy ygyyg yyygy yyggg ggygg ggyyg yyyyy ggggg ygygy gyggg ggyyy gyyyg yyygy ggggy ygggy yyggg gyyyg yyyyg yyygy yygyy gyyyg ygygy yygyy yyygy yygyg ygygy gyyyg yyygy yygyy ygygy yyyyg yyyyy gygyy yygyy yygyy gyyyg yyygy yygyg yygyy ygyyy ygyyy gygyy ggygy yygyy ygyyy gyyyg ygyyy yygyy yyygy ygyyy gyyyy gyyyg yyygy ygyyy gygyy ygyyy gggyg yyygy gyyyg ygyyy gyygg gyygy yygyg yyyyy ggggy ygyyy gyggg yyggg ygyyy gyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy gyyyg yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yygyy ygygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg ygyyy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy gygyg ygygy yygyy gggyg ggygg gyggg ygggy gggyg ggygg gyggg ygggy gggyg ggygg gyggg ygggy gggyg ggygg gyggg yygyy ygyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyy gyyyg yyygy yygyy ygyyg yyahi beuse dgfnr thegl iwvta lesio dyfnr theyh ateno esatt nnvme iyhal etnfa puren utthi tthef raexe asste pionp richk wnowe ilaop theve kynrd fnrth ewach eraow hicte rfabe yathn utjnd aeaim ontsu reyhe retnp noeqt there ireno lktyn cnssa galat aesis firis awios eegut ahibe onade iyher enoen fthem asiod isfir isawi otell onnoe elsev onyse ather snapu essay allhe idfnr nlkmc aisee knuth ere\n"
+      "mirbi mxnda ehisg noeai rrave daose lwuki sclio oedpu tshed adotm iketh ereod ejvnu sthel nwils titan othao kshem ibhiv epeeo kadoi ccedp utthe rehis peeoo nrios nmdem iodio dther eason thaog muwht ngnno herrn nmhid peeor iosiw kedio dshel efton messi geput snmet haogt ellsm eshea snkio dyesh nuldm nveno athao kshem ibhiv eclio oedth asmei oyhal eahiv ethed nwume otxnd aeyio tedfr nmlno dnoad notko nyhny sheko eyput thefa fthwh icter yisha ddeoa oiwiv atbao istno elaot elaos nmere miaos aothe eches usrnn mitth eprat ashmu seuma tyise owrbc tedus aogio nther cnlbi lchip etaww acher aoveo tedao theoa oetee othwe oturb aiske dthem useum tntes tatio dthed nwume otasi utheo tawsn agues sthea mcera ilwac hersw hnnlr eillb hidmn vedno cerhi csash nuldo thive peeos urcra sedif terth edasw nverb itpnd rumpu tatle ivesm eynod eraog yhite lseth ebdev elnce dnoen ddfei turey isifr aejer uooao grnuo dthre esade snfth elaot elwno sasta ognfi saogl ernyn fpliw kiody hatet alest hitth emuse umtha okyer eidde daoth ernmi ocera ndthe breid aoseq ueowe yyppy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp pyyyp yypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypyyp yyypy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yypyy ypyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy ypyyy pyyyy pyyyp ppyyy yyppp yyypy yypyy ypyyy pyyyy pppyy pyyyp ypppy yyppp pyypp pyyyp ppyyp ppypy yypyy yypyp yypyy pyyyp yyyyy pypyy ppyyp yypyp yyyyp yyypp yppyp yypyy pyyyy ypyyp yyypy yyypy yypyy ypyyp yyypy pyyyp ypyyy yypyy ypypy pypyp yyypy yypyy ypypy pypyy pyypy yyyyp yyypy ypyyy ypyyp yyypy yyppp ppypp ppyyp yyyyy ppppp ypypy pyppp ppyyy pyyyp yyypy ppppy ypppy yyppp pyyyp yyyyp yyypy yypyy pyyyp ypypy yypyy yyypy yypyp ypypy pyyyp yyypy yypyy ypypy yyyyp yyyyy pypyy yypyy yypyy pyyyp yyypy yypyp yypyy ypyyy ypyyy pypyy ppypy yypyy ypyyy pyyyp ypyyy yypyy yyypy ypyyy pyyyy pyyyp yyypy ypyyy pypyy ypyyy pppyp yyypy pyyyp ypyyy pyypp pyypy yypyp yyyyy ppppy ypyyy pyppp yyppp ypyyy pyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy pyyyp yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yypyy ypypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp ypyyy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy yypyy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypppy pppyp ppypp pyppp yypyy ypyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyp yyahi veuse dpfnr thepl iwkta lesio dyfnr theyh ateno esatt nnkme iyhal etnfa guren utthi tthef raeje asste giong richb wnowe ilaog theke bynrd fnrth ewach eraow hicte rfave yathn utxnd aeaim ontsu reyhe retng noezt there ireno lbtyn cnssa palat aesis firis awios eeput ahive onade iyher enoen fthem asiod isfir isawi otell onnoe elsek onyse ather snagu essay allhe idfnr nlbmc aisee bnuth ere\n"
      ]
     }
    ],
   },
   {
    "cell_type": "code",
-   "execution_count": 20,
+   "execution_count": 23,
    "metadata": {},
    "outputs": [
     {
     " 'Z': 'f'}\n",
     "\n",
     "tt = ''.maketrans(trans)\n",
-    "print('\\n'.join(tpack(segment(sanitise(ca.translate(tt))))))"
+    "print(prettify(sanitise(ca.translate(tt))))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 21,
+   "execution_count": 17,
    "metadata": {},
    "outputs": [
     {
        " 'Z': 'f'}"
       ]
      },
-     "execution_count": 21,
+     "execution_count": 17,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 22,
+   "execution_count": 18,
    "metadata": {},
    "outputs": [
     {
        " 'z': 'X'}"
       ]
      },
-     "execution_count": 22,
+     "execution_count": 18,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 23,
+   "execution_count": 24,
    "metadata": {},
    "outputs": [
     {
     }
    ],
    "source": [
-    "print('\\n'.join(tpack(segment(keyword_decipher(sanitise(ca), 'decoy', KeywordWrapAlphabet.from_last)))))"
+    "print(prettify(keyword_decipher(sanitise(ca), ka[0], ka[1])))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 24,
+   "execution_count": 25,
    "metadata": {},
    "outputs": [
     {
        "3362"
       ]
      },
-     "execution_count": 24,
+     "execution_count": 25,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "open('5a.plaintext', 'w').write('\\n'.join(tpack(segment(sanitise(ca.translate(tt))))))"
+    "open('5a.plaintext', 'w').write(prettify(keyword_decipher(sanitise(ca), ka[0], ka[1])))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 25,
+   "execution_count": 26,
    "metadata": {},
    "outputs": [],
    "source": [
   },
   {
    "cell_type": "code",
-   "execution_count": 26,
+   "execution_count": 27,
    "metadata": {},
    "outputs": [
     {
        "1577"
       ]
      },
-     "execution_count": 26,
+     "execution_count": 27,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 27,
+   "execution_count": 28,
    "metadata": {},
    "outputs": [
     {
        "1577"
       ]
      },
-     "execution_count": 27,
+     "execution_count": 28,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 49,
+   "execution_count": 29,
    "metadata": {},
    "outputs": [
     {
        " '  ⌷  ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷  ⌷  ']"
       ]
      },
-     "execution_count": 49,
+     "execution_count": 29,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 29,
+   "execution_count": 30,
    "metadata": {},
    "outputs": [],
    "source": [
   },
   {
    "cell_type": "code",
-   "execution_count": 30,
+   "execution_count": 31,
    "metadata": {},
    "outputs": [
     {
        "('fwruffnquevmp', -1750.4576000271347)"
       ]
      },
-     "execution_count": 30,
+     "execution_count": 31,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 31,
+   "execution_count": 32,
    "metadata": {},
    "outputs": [
     {
        "'nieyqmhcenwlqvchdporosweiohtsshreoihpsvrvusdcostmjezkdeytdhrtasupknropbklngeutgnssthjhhnowgbdwvucncdvofhbdltfpvobiuicirayodpwkpoutibtgcenuhpwachchnbrjydhsfvhqszeuvdcpswatvzmxhsiigkobvcagqcdikfstrvkfggbdkvcvstuvufienwiniasbcecmvdwemkwgtnsyjridowjamhuhnrvxssgewikqxpxhashvwvteulpavioctweavlhtikkkximoikdloikrwswfiojrynulrsgtoktknqcporlruoigivdpeddhrntdmrnucnrioiuetpdnftaipjrewjqtercxzvydhiswrtrneonknnfsyrifcdhedoweptjrechehcptdcehirohbcpniloorntrrrtohgeoandszehilxywfqkrgnphglvcojfbetcietpqmfetkalnahiwixdppersmrihrctseahkhtifbokasoozrjgvvkehjsufarunbrtiswzdmriwcrovhdsdeihdteiiustmjupvkhdiisibjloyrdpyxkusdlwfpsehvevdoiudppehzgvcewnhifrvkfggbdkbidptartnotloihpseehrdipeotmlwbcousicucctsktsrlritcanhpdmrehuinioiuuwirsxtanvjshfwyrkacfihfctdipytegvprdowhfldhycrqhfctswdbnagevitctsufsrofoccprycjgnifeikrthnowgcedfedjwiisdtihszeecfvlwjqgehoeicssaqflthagmncpvvifnreneoiioiulytddhuhkrtzsdpevphodehkctsvfhltophksiepsgcohnrnuseajettlhdwhiepjwcenbtbuecihpijncohouidiqjikpovimvsrfridohvchdporhrmhlpnikpacvjiswwgszeyxrcpragclkvxbporlruoigoviferifridicrskiporsyypwgprszenephnicczvttucvqesuiivqleesvfrhcfgchuswrocjslotcnxtiiswkoieatruninitradpaortylcouoahdpehgukcvlrmrafrntaovetrxvyinpenvniquglltisxgy'"
       ]
      },
-     "execution_count": 31,
+     "execution_count": 32,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 32,
+   "execution_count": 33,
    "metadata": {},
    "outputs": [
     {
        "('arcanaimperii', -1506.8637359274674)"
       ]
      },
-     "execution_count": 32,
+     "execution_count": 33,
      "metadata": {},
      "output_type": "execute_result"
     }
     "kb, score"
    ]
   },
-  {
-   "cell_type": "code",
-   "execution_count": 33,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'inhisjournaldatedtheidesofoctoberintheyearoftheconsulshipsofcaeceliustulliuscapitopomponianusplotiusfirmusandgaiuscorneliusgallicanusagricolawrotethemysteryofthebattleatcamulodonumisatlastsolvedcalgacusmaybeabarbariannowbuthewasaromancitizenthenwhobetrayedusallforloveofabarbarianithastakenallmyskillsasaleaderofmentokeephimalivethelegionnairesspendtheireveningsdesigningnewandcruelwaystoexecutehiminrevengefortheshamehebroughtuponusbuthislifeispreciousitistheonlycardlefttoplayinoursearchforsalvationandthereturnofthestolenaquilaeifwecanalsorecoverthecodexthenperhapsitslosscanbeconcealedandourliveswillbesparedreleasingtheromantraitorcalgacusmusthavestuckintheproudagricolasthroatbuthemadeapactwiththeremainingcaledoniiandtravellednorthtoexchangetheprisonerfortheaquilaeandthecodexbutthecunningcaledoniantribesmansetanothertrapandpresentedagricolawithaforgerycunninglyassembledwithpagesfromthebooksstolenwhenthetriberansackedmonsgraupiusfortoolongthesonsofromehadunderestimatedthepeopleinbritanniaandwhiletheaquilaofthelegionhadbeenrestoredbytheexchangetheirhonourwasnotagricolafacedareturntoromehumiliationandalmostcertaindeaththesixthchapterofmytaleofwoeisguardedbylightningbullandoak'"
-      ]
-     },
-     "execution_count": 33,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "beaufort_decipher(sanitise(cb), kb)"
-   ]
-  },
   {
    "cell_type": "code",
    "execution_count": 34,
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'a'"
-      ]
-     },
-     "execution_count": 34,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "unpos(25 - pos('z'))"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 35,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'zyxwvutsrqponmlkjihgfedcba'"
-      ]
-     },
-     "execution_count": 35,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "cat(unpos(25 - pos(l)) for l in string.ascii_lowercase)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 36,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'hveheifhbiicuzbbcfgvvnzafwnkqnndivxopvpdionrsyrmjvjlkaehbrfsmvmtvkqrrftcyspatroqqnbndcyiqretamkaszhcaqidtaxmpfrvenkfimmihgrxnvgqtrmcpzsqzpygininbbstddlcomipnnqgqargdgmrkbijtxnubxpuzjzbizesjbvqmutztdzotjzktjvraioamqznxiwnstbedizjnbjurebqqhldegrzvnynjbsdzprnparcknlqxnmryaisrqjxquzevdcijkzsbmmkczinszckaxrmkvnnzzfwlmksajvrzcqdrkqsdfgvyobqfemifqqrjcoilkydqodhdevxbauvrqdmumxaorieubzrdxvrlcowvodkrsqgzzsizvmdvddrnigrzayrlmednqfhqcblgnvrrntkusntzwdqpoddtvypazkhaqgqneyixiiukmcsfngyktqeftzpdmqtupjzjrkdxsunenxybyvqmqtdmfsdcmjkntbmmplvvvmzwvmzzzzcfymmtfumostdtbnzfkydvmddgrccdbjcnapbmmsrmjvtvztbcmmubemtzmdathxksrckiivczbkqzjvxbbyvqwjzzsinsowitztdzotjzenbyrumpsgbbvxopvgqwrcmfivmjtrjsponmsshdcmdrcmxombkjsopkydzblmhevxbkrcdllmuhranozrmdtidpmfedcbfvwkezzfvgrzxizrwkddefedcmrhtqizqzeqdcmtfcmuagskuoxceehvdbmkvqyiqreszfaqrdnxnmkrmwqgqqkekkieuozbrqmkrnvoizbwizihkukawixdzvbgmevxbtprrablnkvqgdbygzobrrqfzdcmsfnsprfncrxrpvespbsdhsrbvvjrbsbcynefqmilghcpeaqkbygwexspbramjbpmwdvgiatzcveonbzdzbbcfgvcojxcvhvyqusraxdireceehxdkuovylzkilefgvyobqfegiaaqdeeonbfidlyxfgvrhxprefmqgqhiuyiwlivipmasrtbdkfcznxbqcreoocieswonydyhwdtzrsqlmmmunvhwjkbmosmheqovbykgmphjsyprvxkvqwclksryojnnfdqpfgziqoyjpchoeszhetlptcrmllzw'"
-      ]
-     },
-     "execution_count": 36,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "rscb = cat(unpos(25 - pos(l)) for l in scb)\n",
-    "rscb"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 37,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "('zixzmzrnkvirr', -1506.8637359274674)"
-      ]
-     },
-     "execution_count": 37,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "kb, score = vigenere_frequency_break(rscb)\n",
-    "kb, score"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 38,
-   "metadata": {},
    "outputs": [
     {
      "data": {
        "'inhisjournaldatedtheidesofoctoberintheyearoftheconsulshipsofcaeceliustulliuscapitopomponianusplotiusfirmusandgaiuscorneliusgallicanusagricolawrotethemysteryofthebattleatcamulodonumisatlastsolvedcalgacusmaybeabarbariannowbuthewasaromancitizenthenwhobetrayedusallforloveofabarbarianithastakenallmyskillsasaleaderofmentokeephimalivethelegionnairesspendtheireveningsdesigningnewandcruelwaystoexecutehiminrevengefortheshamehebroughtuponusbuthislifeispreciousitistheonlycardlefttoplayinoursearchforsalvationandthereturnofthestolenaquilaeifwecanalsorecoverthecodexthenperhapsitslosscanbeconcealedandourliveswillbesparedreleasingtheromantraitorcalgacusmusthavestuckintheproudagricolasthroatbuthemadeapactwiththeremainingcaledoniiandtravellednorthtoexchangetheprisonerfortheaquilaeandthecodexbutthecunningcaledoniantribesmansetanothertrapandpresentedagricolawithaforgerycunninglyassembledwithpagesfromthebooksstolenwhenthetriberansackedmonsgraupiusfortoolongthesonsofromehadunderestimatedthepeopleinbritanniaandwhiletheaquilaofthelegionhadbeenrestoredbytheexchangetheirhonourwasnotagricolafacedareturntoromehumiliationandalmostcertaindeaththesixthchapterofmytaleofwoeisguardedbylightningbullandoak'"
       ]
      },
-     "execution_count": 38,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "vigenere_decipher(rscb, kb)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 39,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'arcanaimperii'"
-      ]
-     },
-     "execution_count": 39,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "cat(unpos(25 - pos(l)) for l in kb)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 40,
-   "metadata": {},
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "in his journal dated the ides of october in the year of the consulships of caec eli us tullius\n",
-      "capito pompon ian us plot i us firm us and gaius cornelius galli can us agricola wrote the mystery\n",
-      "of the battle at camu lo donum is at last solved cal gac us maybe a barbarian now but he was a roman\n",
-      "citizen then who betrayed us all for love of a barbarian it has taken all my skills as a leader of\n",
-      "men to keep him alive the legionnaires spend their evenings designing new and cruel ways to execute\n",
-      "him in revenge for the shame he brought upon us but his life is precious it is the only card left to\n",
-      "play in our search for salvation and the return of the stolen aquilae if we can also recover the\n",
-      "codex then perhaps its loss can be concealed and our lives will be spared releasing the roman\n",
-      "traitor cal gac us must have stuck in the proud agricola s throat but he made a pact with the\n",
-      "remaining caledon ii and travelled north to exchange the prisoner for the aquilae and the codex but\n",
-      "the cunning caledonian tribesman set another trap and presented agricola with a forgery cunningly\n",
-      "assembled with pages from the books stolen when the tribe ransacked mons grau pius for too long the\n",
-      "sons of rome had underestimated the people in britannia and while the aquila of the legion had been\n",
-      "restored by the exchange their honour was not agricola faced a return to rome humiliation and almost\n",
-      "certain death the sixth chapter of my tale of woe is guarded by lightning bull and oak\n"
-     ]
-    }
-   ],
-   "source": [
-    "print('\\n'.join(tpack(segment(vigenere_decipher(rscb, kb)))))"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 41,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "1465"
-      ]
-     },
-     "execution_count": 41,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "open('5b.plaintext', 'w').write('\\n'.join(tpack(segment(vigenere_decipher(rscb, kb)))))"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 35,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "# def beaufort_encipher(message, keyword):\n",
-    "#     \"\"\"Beaufort encipher\n",
-    "\n",
-    "#     >>> beaufort_encipher('inhisjournaldatedtheidesofoctober', 'arcanaimperii')\n",
-    "#     'sevsvrusyrrxfayyxuteemazudmpjmmwr'\n",
-    "#     \"\"\"\n",
-    "#     shifts = [pos(l) for l in sanitise(keyword)]\n",
-    "#     pairs = zip(message, cycle(shifts))\n",
-    "#     return cat([unpos(k - pos(l)) for l, k in pairs])"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 36,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'sevsvrusyrrxfayyxuteemazudmpjmmwr'"
-      ]
-     },
-     "execution_count": 36,
+     "execution_count": 34,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "# beaufort_encipher('inhisjournaldatedtheidesofoctober', 'arcanaimperii')"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 37,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "# beaufort_decipher = beaufort_encipher"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 42,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "# beaufort_decipher('sevsvrusyrrxfayyxuteemazudmpjmmwr', 'arcanaimperii')"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 43,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "# def beaufort_sub_break(message, fitness=Pletters):\n",
-    "#     best_shift = 0\n",
-    "#     best_fit = float('-inf')\n",
-    "#     for key in range(26):\n",
-    "#         plaintext = [unpos(key - pos(l)) for l in message]\n",
-    "#         fit = fitness(plaintext)\n",
-    "#         logger.debug('Beaufort sub break attempt using key {0} gives fit of {1} '\n",
-    "#                      'and decrypt starting: {2}'.format(key, fit,\n",
-    "#                                                         plaintext[:50]))\n",
-    "#         if fit > best_fit:\n",
-    "#             best_fit = fit\n",
-    "#             best_key = key\n",
-    "#     logger.info('Beaufort sub break best fit: key {0} gives fit of {1} and '\n",
-    "#                 'decrypt starting: {2}'.format(best_key, best_fit, \n",
-    "#                     cat([unpos(best_key - pos(l)) for l in message[:50]])))\n",
-    "#     return best_key, best_fit"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 44,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "# def beaufort_frequency_break(message, max_key_length=20, fitness=Pletters):\n",
-    "#     \"\"\"Breaks a Beaufort cipher with frequency analysis\n",
-    "\n",
-    "#     >>> beaufort_frequency_break(beaufort_encipher(sanitise(\"It is time to \" \\\n",
-    "#             \"run. She is ready and so am I. I stole Daniel's pocketbook this \" \\\n",
-    "#             \"afternoon when he left his jacket hanging on the easel in the \" \\\n",
-    "#             \"attic. I jump every time I hear a footstep on the stairs, \" \\\n",
-    "#             \"certain that the theft has been discovered and that I will \" \\\n",
-    "#             \"be caught. The SS officer visits less often now \" \\\n",
-    "#             \"that he is sure\"), 'florence')) # doctest: +ELLIPSIS\n",
-    "#     ('florence', -307.5473096791...)\n",
-    "#     \"\"\"\n",
-    "#     def worker(message, key_length, fitness):\n",
-    "#         splits = every_nth(message, key_length)\n",
-    "#         key = cat([unpos(beaufort_sub_break(s)[0]) for s in splits])\n",
-    "#         plaintext = beaufort_decipher(message, key)\n",
-    "#         fit = fitness(plaintext)\n",
-    "#         return key, fit\n",
-    "#     sanitised_message = sanitise(message)\n",
-    "#     results = starmap(worker, [(sanitised_message, i, fitness)\n",
-    "#                                for i in range(1, max_key_length+1)])\n",
-    "#     return max(results, key=lambda k: k[1])"
+    "beaufort_decipher(sanitise(cb), kb)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 45,
+   "execution_count": 47,
    "metadata": {},
    "outputs": [
     {
        "('arcanaimperii', -1506.8637359274674)"
       ]
      },
-     "execution_count": 45,
+     "execution_count": 47,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 46,
+   "execution_count": 48,
    "metadata": {},
    "outputs": [
     {
     "print(lcat(tpack(repunctuate(pb, pub).split())))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 49,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1470"
+      ]
+     },
+     "execution_count": 49,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "open('5b.plaintext', 'w').write(lcat(tpack(repunctuate(pb, pub).split())))"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.5.3"
+   "version": "3.6.3"
   }
  },
  "nbformat": 4,