Updated notebooks for new library organisation
[cipher-tools.git] / 2017 / 2017-challenge5.ipynb
index f2aa4c5111789da9ffc44b02800e6289a8cb77de..609d0179f5dfaeae78719a2e2e632a4cd50ad142 100644 (file)
     "import matplotlib.pyplot as plt\n",
     "%matplotlib inline\n",
     "\n",
-    "from cipherbreak 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",
@@ -55,7 +45,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 3,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -83,7 +73,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [
     {
@@ -92,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": [
     {
     {
      "data": {
       "text/plain": [
-       "'etoainhsrdlumwycfgpbvkxjqz'"
+       "(('decoy', <KeywordWrapAlphabet.from_last: 2>), -1505.1870286708981)"
       ]
      },
      "execution_count": 9,
     }
    ],
    "source": [
-    "ltrs = [p[0] for p in english_counts.most_common()]\n",
-    "cat(ltrs)"
+    "ka, score = keyword_break_mp(sca_text)\n",
+    "ka, score"
    ]
   },
   {
     {
      "data": {
       "text/plain": [
-       "'yrkfdlbqpoisjcumzaetwhgxnv'"
+       "('decoyzabfghijklmnpqrstuvwx', -4117.559010215594)"
       ]
      },
      "execution_count": 10,
      "output_type": "execute_result"
     }
    ],
+   "source": [
+    "cipher_alphabet, score = simulated_annealing_break(\n",
+    "    sca_text, workers=12,\n",
+    "    initial_temperature=200, max_iterations=20000, \n",
+    "#     plain_alphabet=cat(ltrs), cipher_alphabet=cat(ctls),\n",
+    "    fitness=Ptrigrams)\n",
+    "cipher_alphabet, score"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "maryam jodie has gone i arrived in selcuk as planned but she didnt make the rendezvous the local\n",
+      "station think she may have been kidnapped but there has been no ransom demand and there is nothing\n",
+      "much to go on her room had been ransacked and she left no message but something tells me she is ok\n",
+      "and we should move on i think she may have planned this meanwhile i have the document jodie wanted\n",
+      "from london i dont know how she knew but the fifth chapter was hidden in a cavity in a stone lintel\n",
+      "in some remains in the ephesus room at the british museum it was encrypted using another poly\n",
+      "alphabetic cipher invented in the nineteenth century i asked the museum to test it and the document\n",
+      "is authentic so i guess the imperial cipher school really had moved on perhaps i shouldnt have been\n",
+      "surprised after the discovery at bodrum but it leaves me wondering what else they developed one odd\n",
+      "feature was a frieze running round three sides of the lintel consisting of a single row of\n",
+      "blackandwhite tiles that the museum think were added in the roman period they read in sequence\n",
+      "wwbbwwwbwwwbwwwbwwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb www bbw\n",
+      "wwbw wbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwwb www\n",
+      "bwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb wwwb wwwb wwwb\n",
+      "wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb www\n",
+      "www www www www www www www www www www www www www www www www www www www www www www www www wwbw\n",
+      "wwbw www www www www www www www www www www www www www www www www www www www www www www www www\n",
+      "www wwwb wwwb www wb wwwb bbw wwww bbb wwwb wwwb wwwb www bw www bbb wwbw wwbw bbb www bbbb\n",
+      "wwbbbwwwbbbwwbbbwb www bwwwwbwbwwbwwbwwwbww www bwbwwbbwwbwwbwbwwwwb www bbw bbw\n",
+      "bwwbwwbwwwwwbwwbwwwb www wb www bwwwbwwbwwwbwbwwwbwb www wwbwwwbwbwbwbwbwwwb www bwwwbwbwbwbwwbwwbww\n",
+      "www bwwwbwwbwwwwbwwbwwwb www bbb bbw bbbb wwbw wwww bbb bbw bwbwbwbbbbbwwwbwwwbw wwbw bbbb ww bbb\n",
+      "www bbbb wwwb www wb www bwwwbwwbwwwbwbwbwwwb www wwbwwwbwbwbwbwbwwwb wwwb wwwb www bwb www wwbw\n",
+      "wwww bwbwwwwbwwwwbwwbwwwb www bwwwbwbwwbwwwbwwwwb www bwbwwbbwbwwwbwwwb wwwb www bwb www wwbw www wb\n",
+      "wwbw wwbw www bwwwbwwwbwwbwwwbwb wwwb www bbb wbwwwbwbwwwbwbwwwbww bbb wwbw wwbw bww www bbbb wwbw\n",
+      "wwbwbbbwwbbbwbwwwbww www www www www www www www www www www www www www www www www www www www www\n",
+      "www www www www www wwbw wwbw www www www www www www www www www www www www www www www www www\n",
+      "www www www www www www www www wbwwwbwbwwwbwwwbwwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb\n",
+      "wwwb wwwb wwwb wwwb wwwb wwwb www bwb www bwbwbwbwbwbwbwbwbwb wbwbwbwbwbwbwbwbwbwb\n",
+      "wbwbwbwbwbwbwbwbwbwb wbwbwbwbwbwbwbwbwbwb www bwwbbbwbbbwbbbwbbbwb bbw bbb wbbbwbbbwbbbwbbbwb bbw\n",
+      "bbb wbbbwbbbwbbbwbbbwb bbwbbbwbbbwwbwwwbwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb\n",
+      "wwwb wwwb wwwb www bwwwbwwwbwwwbwwbwwi have used b for the black tiles and w for the white ones it\n",
+      "took me a while to figure out that the frieze is steganography concealing the keyword for the cipher\n",
+      "in chapter five without jodie i am not sure where to go next there are only two possibilities as far\n",
+      "as i can see but i have no idea where one of them is and as far as i can tell no one else knows\n",
+      "either so i guess i will head for olympia see you there\n"
+     ]
+    }
+   ],
+   "source": [
+    "cipher_translation = ''.maketrans(cipher_alphabet, string.ascii_lowercase)\n",
+    "print(prettify(sanitise(ca).translate(cipher_translation)))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'etoainhsrdlumwycfgpbvkxjqz'"
+      ]
+     },
+     "execution_count": 12,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "ltrs = [p[0] for p in english_counts.most_common()]\n",
+    "cat(ltrs)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'yrkfdlbqpoisjcumzaewthgxnv'"
+      ]
+     },
+     "execution_count": 13,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
    "source": [
     "ctls =  [p[0] for p in collections.Counter(sanitise(ca_text)).most_common()]\n",
     "cat(ctls)"
   },
   {
    "cell_type": "code",
-   "execution_count": 11,
+   "execution_count": 14,
    "metadata": {},
    "outputs": [
     {
        " 'Q': 's',\n",
        " 'R': 't',\n",
        " 'S': 'u',\n",
-       " 'T': 'b',\n",
+       " 'T': 'v',\n",
        " 'U': 'y',\n",
        " 'V': 'z',\n",
-       " 'W': 'v',\n",
+       " 'W': 'b',\n",
        " 'X': 'j',\n",
        " 'Y': 'e',\n",
        " 'Z': 'f'}"
       ]
      },
-     "execution_count": 11,
+     "execution_count": 14,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 12,
+   "execution_count": 15,
    "metadata": {},
    "outputs": [
     {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "mirvi mxnda ehisg noeai rrabe daose lwuki sclio oedpu tshed adotm iketh ereod ejbnu sthel nwils titan othao kshem ivhib 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 nbeno athao kshem ivhib eclio oedth asmei oyhal eahib ethed nwume otxnd aeyio tedfr nmlno dnoad notko nyhny sheko eyput thefa fthwh icter yisha ddeoa oiwib atvao istno elaot elaos nmere miaos aothe eches usrnn mitth eprat ashmu seuma tyise owrvc tedus aogio nther cnlvi lchip etaww acher aobeo tedao theoa oetee othwe oturv aiske dthem useum tntes tatio dthed nwume otasi utheo tawsn agues sthea mcera ilwac hersw hnnlr eillv hidmn bedno cerhi csash nuldo thibe peeos urcra sedif terth edasw nberv itpnd rumpu tatle ibesm eynod eraog yhite lseth evdeb 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 vreid 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 beuse dpfnr thepl iwkta lesio dyfnr theyh ateno esatt nnkme iyhal etnfa guren utthi tthef raeje asste giong richv wnowe ilaog theke vynrd fnrth ewach eraow hicte rfabe yathn utxnd aeaim ontsu reyhe retng noezt there ireno lvtyn cnssa palat aesis firis awios eeput ahibe onade iyher enoen fthem asiod isfir isawi otell onnoe elsek onyse ather snagu essay allhe idfnr nlvmc aisee vnuth 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": 13,
+   "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": 14,
+   "execution_count": 17,
    "metadata": {},
    "outputs": [
     {
        " 'Z': 'f'}"
       ]
      },
-     "execution_count": 14,
+     "execution_count": 17,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 18,
    "metadata": {},
    "outputs": [
     {
        " 'z': 'X'}"
       ]
      },
-     "execution_count": 15,
+     "execution_count": 18,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "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": 17,
+   "execution_count": 25,
    "metadata": {},
    "outputs": [
     {
        "3362"
       ]
      },
-     "execution_count": 17,
+     "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": 18,
+   "execution_count": 26,
    "metadata": {},
    "outputs": [],
    "source": [
   },
   {
    "cell_type": "code",
-   "execution_count": 19,
+   "execution_count": 27,
    "metadata": {},
    "outputs": [
     {
        "1577"
       ]
      },
-     "execution_count": 19,
+     "execution_count": 27,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 20,
+   "execution_count": 28,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "26"
+       "1577"
       ]
      },
-     "execution_count": 20,
+     "execution_count": 28,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "len(set(sanitise(cb)))"
+    "19 * 83"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 21,
+   "execution_count": 29,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "[('n', 74),\n",
-       " ('i', 72),\n",
-       " ('a', 70),\n",
-       " ('w', 65),\n",
-       " ('e', 64),\n",
-       " ('j', 62),\n",
-       " ('y', 61),\n",
-       " ('m', 55),\n",
-       " ('r', 54),\n",
-       " ('p', 49),\n",
-       " ('v', 49),\n",
-       " ('x', 48),\n",
-       " ('h', 47),\n",
-       " ('g', 41),\n",
-       " ('l', 38),\n",
-       " ('u', 38),\n",
-       " ('k', 35),\n",
-       " ('b', 34),\n",
-       " ('c', 34),\n",
-       " ('z', 33),\n",
-       " ('t', 33),\n",
-       " ('q', 32),\n",
-       " ('s', 29),\n",
-       " ('f', 25),\n",
-       " ('o', 24),\n",
-       " ('d', 22)]"
+       "['  ⌷⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷⌷  ',\n",
+       " ' ⌷  ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷  ⌷ ',\n",
+       " '  ⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷  ',\n",
+       " ' ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷ ',\n",
+       " '  ⌷                                                                             ⌷  ',\n",
+       " ' ⌷                                                                               ⌷ ',\n",
+       " '  ⌷    ⌷   ⌷⌷⌷     ⌷⌷⌷   ⌷   ⌷   ⌷   ⌷    ⌷⌷⌷  ⌷   ⌷ ⌷⌷⌷   ⌷⌷⌷⌷  ⌷⌷⌷   ⌷⌷⌷  ⌷⌷⌷ ⌷  ',\n",
+       " ' ⌷    ⌷ ⌷  ⌷  ⌷   ⌷     ⌷ ⌷  ⌷⌷  ⌷  ⌷ ⌷    ⌷   ⌷⌷ ⌷⌷ ⌷  ⌷  ⌷     ⌷  ⌷   ⌷    ⌷   ⌷ ',\n",
+       " '  ⌷  ⌷   ⌷ ⌷   ⌷ ⌷     ⌷   ⌷ ⌷ ⌷ ⌷ ⌷   ⌷   ⌷   ⌷ ⌷ ⌷ ⌷  ⌷  ⌷     ⌷   ⌷  ⌷    ⌷  ⌷  ',\n",
+       " ' ⌷   ⌷⌷⌷⌷⌷ ⌷⌷⌷⌷  ⌷     ⌷⌷⌷⌷⌷ ⌷ ⌷ ⌷ ⌷⌷⌷⌷⌷   ⌷   ⌷   ⌷ ⌷⌷⌷⌷  ⌷⌷⌷   ⌷⌷⌷⌷   ⌷    ⌷   ⌷ ',\n",
+       " '  ⌷  ⌷   ⌷ ⌷ ⌷   ⌷     ⌷   ⌷ ⌷ ⌷ ⌷ ⌷   ⌷   ⌷   ⌷   ⌷ ⌷     ⌷     ⌷ ⌷    ⌷    ⌷  ⌷  ',\n",
+       " ' ⌷   ⌷   ⌷ ⌷  ⌷   ⌷    ⌷   ⌷ ⌷  ⌷⌷ ⌷   ⌷   ⌷   ⌷   ⌷ ⌷     ⌷     ⌷  ⌷   ⌷    ⌷   ⌷ ',\n",
+       " '  ⌷  ⌷   ⌷ ⌷   ⌷   ⌷⌷⌷ ⌷   ⌷ ⌷   ⌷ ⌷   ⌷  ⌷⌷⌷  ⌷   ⌷ ⌷     ⌷⌷⌷⌷  ⌷   ⌷ ⌷⌷⌷  ⌷⌷⌷ ⌷  ',\n",
+       " ' ⌷                                                                               ⌷ ',\n",
+       " '  ⌷                                                                             ⌷  ',\n",
+       " ' ⌷ ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷ ⌷ ',\n",
+       " '  ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷ ⌷  ',\n",
+       " ' ⌷  ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷ ⌷⌷⌷  ⌷ ',\n",
+       " '  ⌷  ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷   ⌷  ⌷  ']"
       ]
      },
-     "execution_count": 21,
+     "execution_count": 29,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "collections.Counter(sanitise(cb)).most_common()"
+    "tt = ''.maketrans({'w': ' ', 'b': '⌷'})\n",
+    "chunks(bkeyt.translate(tt), 83)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 22,
+   "execution_count": 30,
    "metadata": {},
    "outputs": [],
    "source": [
   },
   {
    "cell_type": "code",
-   "execution_count": 23,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "[('i', 'n'), ('n', 'o'), ('o', 'o'), ('o', 'a'), ('a', 'd')]"
-      ]
-     },
-     "execution_count": 23,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "[(scb[i-1], scb[i]) for i in range(1, len(scb))][-5:]"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 24,
+   "execution_count": 31,
    "metadata": {},
    "outputs": [
     {
        "('fwruffnquevmp', -1750.4576000271347)"
       ]
      },
-     "execution_count": 24,
+     "execution_count": 31,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 25,
+   "execution_count": 32,
    "metadata": {},
    "outputs": [
     {
        "'nieyqmhcenwlqvchdporosweiohtsshreoihpsvrvusdcostmjezkdeytdhrtasupknropbklngeutgnssthjhhnowgbdwvucncdvofhbdltfpvobiuicirayodpwkpoutibtgcenuhpwachchnbrjydhsfvhqszeuvdcpswatvzmxhsiigkobvcagqcdikfstrvkfggbdkvcvstuvufienwiniasbcecmvdwemkwgtnsyjridowjamhuhnrvxssgewikqxpxhashvwvteulpavioctweavlhtikkkximoikdloikrwswfiojrynulrsgtoktknqcporlruoigivdpeddhrntdmrnucnrioiuetpdnftaipjrewjqtercxzvydhiswrtrneonknnfsyrifcdhedoweptjrechehcptdcehirohbcpniloorntrrrtohgeoandszehilxywfqkrgnphglvcojfbetcietpqmfetkalnahiwixdppersmrihrctseahkhtifbokasoozrjgvvkehjsufarunbrtiswzdmriwcrovhdsdeihdteiiustmjupvkhdiisibjloyrdpyxkusdlwfpsehvevdoiudppehzgvcewnhifrvkfggbdkbidptartnotloihpseehrdipeotmlwbcousicucctsktsrlritcanhpdmrehuinioiuuwirsxtanvjshfwyrkacfihfctdipytegvprdowhfldhycrqhfctswdbnagevitctsufsrofoccprycjgnifeikrthnowgcedfedjwiisdtihszeecfvlwjqgehoeicssaqflthagmncpvvifnreneoiioiulytddhuhkrtzsdpevphodehkctsvfhltophksiepsgcohnrnuseajettlhdwhiepjwcenbtbuecihpijncohouidiqjikpovimvsrfridohvchdporhrmhlpnikpacvjiswwgszeyxrcpragclkvxbporlruoigoviferifridicrskiporsyypwgprszenephnicczvttucvqesuiivqleesvfrhcfgchuswrocjslotcnxtiiswkoieatruninitradpaortylcouoahdpehgukcvlrmrafrntaovetrxvyinpenvniquglltisxgy'"
       ]
      },
-     "execution_count": 25,
+     "execution_count": 32,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 26,
+   "execution_count": 33,
    "metadata": {},
    "outputs": [
     {
        "('arcanaimperii', -1506.8637359274674)"
       ]
      },
-     "execution_count": 26,
+     "execution_count": 33,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 27,
+   "execution_count": 34,
    "metadata": {},
    "outputs": [
     {
        "'inhisjournaldatedtheidesofoctoberintheyearoftheconsulshipsofcaeceliustulliuscapitopomponianusplotiusfirmusandgaiuscorneliusgallicanusagricolawrotethemysteryofthebattleatcamulodonumisatlastsolvedcalgacusmaybeabarbariannowbuthewasaromancitizenthenwhobetrayedusallforloveofabarbarianithastakenallmyskillsasaleaderofmentokeephimalivethelegionnairesspendtheireveningsdesigningnewandcruelwaystoexecutehiminrevengefortheshamehebroughtuponusbuthislifeispreciousitistheonlycardlefttoplayinoursearchforsalvationandthereturnofthestolenaquilaeifwecanalsorecoverthecodexthenperhapsitslosscanbeconcealedandourliveswillbesparedreleasingtheromantraitorcalgacusmusthavestuckintheproudagricolasthroatbuthemadeapactwiththeremainingcaledoniiandtravellednorthtoexchangetheprisonerfortheaquilaeandthecodexbutthecunningcaledoniantribesmansetanothertrapandpresentedagricolawithaforgerycunninglyassembledwithpagesfromthebooksstolenwhenthetriberansackedmonsgraupiusfortoolongthesonsofromehadunderestimatedthepeopleinbritanniaandwhiletheaquilaofthelegionhadbeenrestoredbytheexchangetheirhonourwasnotagricolafacedareturntoromehumiliationandalmostcertaindeaththesixthchapterofmytaleofwoeisguardedbylightningbullandoak'"
       ]
      },
-     "execution_count": 27,
+     "execution_count": 34,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 28,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'a'"
-      ]
-     },
-     "execution_count": 28,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "unpos(25 - pos('z'))"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 29,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'zyxwvutsrqponmlkjihgfedcba'"
-      ]
-     },
-     "execution_count": 29,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "cat(unpos(25 - pos(l)) for l in string.ascii_lowercase)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 30,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'hveheifhbiicuzbbcfgvvnzafwnkqnndivxopvpdionrsyrmjvjlkaehbrfsmvmtvkqrrftcyspatroqqnbndcyiqretamkaszhcaqidtaxmpfrvenkfimmihgrxnvgqtrmcpzsqzpygininbbstddlcomipnnqgqargdgmrkbijtxnubxpuzjzbizesjbvqmutztdzotjzktjvraioamqznxiwnstbedizjnbjurebqqhldegrzvnynjbsdzprnparcknlqxnmryaisrqjxquzevdcijkzsbmmkczinszckaxrmkvnnzzfwlmksajvrzcqdrkqsdfgvyobqfemifqqrjcoilkydqodhdevxbauvrqdmumxaorieubzrdxvrlcowvodkrsqgzzsizvmdvddrnigrzayrlmednqfhqcblgnvrrntkusntzwdqpoddtvypazkhaqgqneyixiiukmcsfngyktqeftzpdmqtupjzjrkdxsunenxybyvqmqtdmfsdcmjkntbmmplvvvmzwvmzzzzcfymmtfumostdtbnzfkydvmddgrccdbjcnapbmmsrmjvtvztbcmmubemtzmdathxksrckiivczbkqzjvxbbyvqwjzzsinsowitztdzotjzenbyrumpsgbbvxopvgqwrcmfivmjtrjsponmsshdcmdrcmxombkjsopkydzblmhevxbkrcdllmuhranozrmdtidpmfedcbfvwkezzfvgrzxizrwkddefedcmrhtqizqzeqdcmtfcmuagskuoxceehvdbmkvqyiqreszfaqrdnxnmkrmwqgqqkekkieuozbrqmkrnvoizbwizihkukawixdzvbgmevxbtprrablnkvqgdbygzobrrqfzdcmsfnsprfncrxrpvespbsdhsrbvvjrbsbcynefqmilghcpeaqkbygwexspbramjbpmwdvgiatzcveonbzdzbbcfgvcojxcvhvyqusraxdireceehxdkuovylzkilefgvyobqfegiaaqdeeonbfidlyxfgvrhxprefmqgqhiuyiwlivipmasrtbdkfcznxbqcreoocieswonydyhwdtzrsqlmmmunvhwjkbmosmheqovbykgmphjsyprvxkvqwclksryojnnfdqpfgziqoyjpchoeszhetlptcrmllzw'"
-      ]
-     },
-     "execution_count": 30,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "rscb = cat(unpos(25 - pos(l)) for l in scb)\n",
-    "rscb"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 31,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "('zixzmzrnkvirr', -1506.8637359274674)"
-      ]
-     },
-     "execution_count": 31,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "kb, score = vigenere_frequency_break(rscb)\n",
-    "kb, score"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 32,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'inhisjournaldatedtheidesofoctoberintheyearoftheconsulshipsofcaeceliustulliuscapitopomponianusplotiusfirmusandgaiuscorneliusgallicanusagricolawrotethemysteryofthebattleatcamulodonumisatlastsolvedcalgacusmaybeabarbariannowbuthewasaromancitizenthenwhobetrayedusallforloveofabarbarianithastakenallmyskillsasaleaderofmentokeephimalivethelegionnairesspendtheireveningsdesigningnewandcruelwaystoexecutehiminrevengefortheshamehebroughtuponusbuthislifeispreciousitistheonlycardlefttoplayinoursearchforsalvationandthereturnofthestolenaquilaeifwecanalsorecoverthecodexthenperhapsitslosscanbeconcealedandourliveswillbesparedreleasingtheromantraitorcalgacusmusthavestuckintheproudagricolasthroatbuthemadeapactwiththeremainingcaledoniiandtravellednorthtoexchangetheprisonerfortheaquilaeandthecodexbutthecunningcaledoniantribesmansetanothertrapandpresentedagricolawithaforgerycunninglyassembledwithpagesfromthebooksstolenwhenthetriberansackedmonsgraupiusfortoolongthesonsofromehadunderestimatedthepeopleinbritanniaandwhiletheaquilaofthelegionhadbeenrestoredbytheexchangetheirhonourwasnotagricolafacedareturntoromehumiliationandalmostcertaindeaththesixthchapterofmytaleofwoeisguardedbylightningbullandoak'"
-      ]
-     },
-     "execution_count": 32,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "vigenere_decipher(rscb, kb)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 33,
+   "execution_count": 47,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "'arcanaimperii'"
+       "('arcanaimperii', -1506.8637359274674)"
       ]
      },
-     "execution_count": 33,
+     "execution_count": 47,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "cat(unpos(25 - pos(l)) for l in kb)"
+    "kb, sb = beaufort_frequency_break(cb)\n",
+    "kb, sb"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 34,
+   "execution_count": 48,
    "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"
+      "In his Journal dated the Ides of October in the year of the consulships of Caecelius Tullius Capito\n",
+      "Pomponianus Plotius Firmus and Gaius Cornelius Gallicanus, Agricola wrote “The mystery of the battle\n",
+      "at Camulodonum is at last solved. Calgacus may be a Barbarian now, but he was a Roman citizen then,\n",
+      "who betrayed us all for love of a barbarian. It has taken all my skills as a leader of men to keep\n",
+      "him alive. The Legionnaires spend their evenings designing new and cruel ways to execute him in\n",
+      "revenge for the shame he brought upon us, but his life is precious. It is the only card left to play\n",
+      "in our search for salvation and the return of the stolen Aquilae. If we can also recover the Codex\n",
+      "then perhaps its loss can be concealed and our lives will be spared.” Releasing the Roman traitor\n",
+      "Calgacus must have stuck in the proud Agricola’s throat, but he made a pact with the remaining\n",
+      "Caledonii and travelled north to exchange the prisoner for the Aquilae and the Codex. But the\n",
+      "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 Graupius. 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\n",
+      "almost 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)))))"
+    "pub = depunctuate(cb)\n",
+    "pb = beaufort_decipher(scb, kb)\n",
+    "print(lcat(tpack(repunctuate(pb, pub).split())))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 35,
+   "execution_count": 49,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "1465"
-      ]
-     },
-     "execution_count": 35,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "open('5b.plaintext', 'w').write('\\n'.join(tpack(segment(vigenere_decipher(rscb, kb)))))"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 36,
-   "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": 37,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'sevsvrusyrrxfayyxuteemazudmpjmmwr'"
-      ]
-     },
-     "execution_count": 37,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "beaufort_encipher('inhisjournaldatedtheidesofoctober', 'arcanaimperii')"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 38,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "beaufort_decipher = beaufort_encipher"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 39,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "'inhisjournaldatedtheidesofoctober'"
-      ]
-     },
-     "execution_count": 39,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
-   "source": [
-    "beaufort_decipher('sevsvrusyrrxfayyxuteemazudmpjmmwr', 'arcanaimperii')"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 40,
-   "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": 41,
-   "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])"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 42,
-   "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "('arcanaimperii', -1506.8637359274674)"
+       "1470"
       ]
      },
-     "execution_count": 42,
+     "execution_count": 49,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "beaufort_frequency_break(cb)"
+    "open('5b.plaintext', 'w').write(lcat(tpack(repunctuate(pb, pub).split())))"
    ]
   },
   {
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.5.2"
+   "version": "3.6.3"
   }
  },
  "nbformat": 4,