Updated notebooks for new library organisation
[cipher-tools.git] / 2017 / 2017-challenge2.ipynb
index 4f5eae39c1053ae85544d8453b91321380ffdf85..ff2624d3c598719330636a7ad855322307929587 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.column_transposition import *\n",
+    "from support.text_prettify import *\n",
     "\n",
     "ca = open('2a.ciphertext').read()\n",
     "cb = open('2b.ciphertext').read()"
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 2,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "(14, -3007.03777360488)"
+       "(14, -3006.9125209110575)"
       ]
      },
-     "execution_count": 7,
+     "execution_count": 2,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -43,7 +47,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 3,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "((5, 16, True), -2842.001592719175)"
+       "((5, 16, True), -2842.062950889474)"
       ]
      },
-     "execution_count": 9,
+     "execution_count": 4,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -86,7 +90,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
+   "execution_count": 5,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 12,
+   "execution_count": 6,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "(('cairo', <KeywordWrapAlphabet.from_largest: 3>), -2538.6915458754715)"
+       "(('cairo', <KeywordWrapAlphabet.from_largest: 3>), -2538.6487026668074)"
       ]
      },
-     "execution_count": 12,
+     "execution_count": 6,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 7,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 8,
    "metadata": {},
    "outputs": [
     {
        "2530"
       ]
      },
-     "execution_count": 15,
+     "execution_count": 8,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": 9,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "(0, -2059.1045635741802)"
+       "(0, -2059.063429379348)"
       ]
      },
-     "execution_count": 16,
+     "execution_count": 9,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 17,
+   "execution_count": 10,
    "metadata": {},
    "outputs": [
     {
        "'tel uutrfetnehioyitbephn da he i ood rul tie rate fre haedugsioafdeiielcgn esr pine oe reo on rhftodcrdvlsrvtdbtm moss an fnsrwturawteernofgvp uhehloastieeaooenxih btn rees tv armed hb lccelrocshnpicctvvbu a esd adam a at ids neat res at mwr oitdteoafutoteeiltiu song lro a ae alle of lts htnirwtalnrdornnef sort y ont frith lee doer do i gewitneeyolivmedttud are on du th gti set tfbrosfnrtmrutlkwg to dinu do by rfr deot tut see e ov yeeeah yau at fdot a i at rec ihfnhpaouerseselupsh lsr to wcg to eu to at raoeceyvteeeiadesmaw aetna ihieysrlemritmtchrei or nh et stop eeg mhi hau at bal of uno ho is hceoirnsdgasbboohaes ileaigageefiwgtyhwuh tue ruts held ei on oe fniienhosasnoueiytih net tsnfbsueihossiurdenl md oh yom re a as i wst gmt nrao ocmfihlnaedcariwrrd tel nsgjerurncsoegttlfca a tri rels sri do eeo ssi knuth ndt ltnribbaadsehyetppbo am thd neg epic nj chf ciypiuireectlsdewodc ter fi set et dnntgabopgsenlaaodc bed htrmoelrserruuevstrw id ftb as bott tah wear o lori bnc kg mft to cree vt seb nmr ur heed see sioylsioeuoioroubtao cneacaadlttnndeobeea niue yu cee dc no in ich mm tte oyaauwerodolstiiheiy pro hltvoueeoisdpuoiohlr hla iaea du loel pepa ahnohtarnuiibcdtsh set uaihulalcnooefawo an rene hhreuvecfdoidotrwlrr tslsa iv uta ebi i hi a heat scc we sdetwihntehettuuohh teidvttereoybcsttehh need tart to dau maes gaed muurveoxnssftnnmiehn note to svbolrhwebuentvdsrs cleb gats us do to a ihl nst nish doo a lee hon rsc pstn i this pdb nc soes ft nei see sebi wtewuretnyltaadeutdd noce ruh a edie le marino c insmod an few si see ocle lroefcldhiaatihoeaw der hls etna cyan mhoaihtftahacoehaes lrtwkeodnltehhonbnqo at hhaeltnjbrnihutinltz ctrn copes mag flo en tures n led an env n he an i eio set r the hm faa co arni uri guoesneeethdntuowpg afc eng us ie it esoeosrtebcefrnhutr fiigs das ah pate due retd berr tec in hey serr end is sdet dmo css pres gei eu ups erath ferfbceoacsmhsefgq at eaettlwttoavteefepao aol as oee et to ie i int fay lo lee fs'"
       ]
      },
-     "execution_count": 17,
+     "execution_count": 10,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 18,
+   "execution_count": 11,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "((1, 0, True), -2059.1045635741802)"
+       "((1, 0, True), -2059.063429379348)"
       ]
      },
-     "execution_count": 18,
+     "execution_count": 11,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 20,
+   "execution_count": 12,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 25,
+   "execution_count": 13,
    "metadata": {},
    "outputs": [
     {
   },
   {
    "cell_type": "code",
-   "execution_count": 39,
+   "execution_count": 20,
    "metadata": {},
    "outputs": [
     {
-     "data": {
-      "text/plain": [
-       "'the rebellion led by queen boudicca took strength from the defeat of the ninth legion the iceni were joined by the trinovantes tribe as they setup on both londinium and verulam ium they razed the cities to the ground and desecrated our temples suetonius informed by varus legatus of the ix legion of the seriousness of the situation returned from subduing rebels on the island of mona and confronted the enemy at viro conium a she travelled back along watling street in his own words despite being outnumbered ten to one the bravery and heroism of our forces and my careful choice of the battleground and tactics ensured our decisive and glorious victory whoever masterminded the battle the enemy lost many thousands against just a few hundred of our own troops and agricola was left to secure the region acting with ruthless efficiency perhaps in part to at one for his guilt over the previous loss he supressed the rebellion boudicca possessed of the perilous ness of her situation took her own life cursing the ixth legion as she died but a battle is not a war and the true peril was about to be revealed a captured enemy spy revealed that they had be enable to decipher captured battle orders and communications which had been encrypted using the caesar cipher this device invented by julius caesar himself had secured military and diplomatic communications across the empire for decades it was no longer safe and neither were we suetonius s despatches to the emperor were careful to give himself the credit for the defeat of boudicca and left no doubt that agricola was to blame for the uprising the loss of the legions aquila was said to have encouraged the local tribes to rebel and it was hinted that agricola may also have been to blame for the loss of the caesar cipher what suetonius failed to reveal was that it was agricola who engineered the defeat of the tribes on watling street but if you wish to know the truth of that day then you will need to travel further to the isle of thorns'"
-      ]
-     },
-     "execution_count": 39,
-     "metadata": {},
-     "output_type": "execute_result"
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "the rebellion led by queen boudicca took strength from the defeat of the ninth legion the iceni were\n",
+      "joined by the trinovantes tribe as they setup on both londinium and verulam ium they razed the\n",
+      "cities to the ground and desecrated our temples suetonius informed by varus legatus of the ix legion\n",
+      "of the seriousness of the situation returned from subduing rebels on the island of mona and\n",
+      "confronted the enemy at viro conium a she travelled back along watling street in his own words\n",
+      "despite being outnumbered ten to one the bravery and heroism of our forces and my careful choice of\n",
+      "the battleground and tactics ensured our decisive and glorious victory whoever masterminded the\n",
+      "battle the enemy lost many thousands against just a few hundred of our own troops and agricola was\n",
+      "left to secure the region acting with ruthless efficiency perhaps in part to at one for his guilt\n",
+      "over the previous loss he supressed the rebellion boudicca possessed of the perilous ness of her\n",
+      "situation took her own life cursing the ixth legion as she died but a battle is not a war and the\n",
+      "true peril was about to be revealed a captured enemy spy revealed that they had be enable to\n",
+      "decipher captured battle orders and communications which had been encrypted using the caesar cipher\n",
+      "this device invented by julius caesar himself had secured military and diplomatic communications\n",
+      "across the empire for decades it was no longer safe and neither were we suetonius s despatches to\n",
+      "the emperor were careful to give himself the credit for the defeat of boudicca and left no doubt\n",
+      "that agricola was to blame for the uprising the loss of the legions aquila was said to have\n",
+      "encouraged the local tribes to rebel and it was hinted that agricola may also have been to blame for\n",
+      "the loss of the caesar cipher what suetonius failed to reveal was that it was agricola who\n",
+      "engineered the defeat of the tribes on watling street but if you wish to know the truth of that day\n",
+      "then you will need to travel further to the isle of thorns\n"
+     ]
     }
    ],
    "source": [
-    "wcat(segment(cat(cat(zr) for zr in zip(*[sanitise(r) for r in cb.split('\\n')]))))"
+    "print(prettify(cat(cat(zr) for zr in zip(*[sanitise(r) for r in cb.split('\\n')]))))"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 24,
+   "execution_count": 15,
    "metadata": {},
    "outputs": [
     {
        "274"
       ]
      },
-     "execution_count": 24,
+     "execution_count": 15,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 33,
+   "execution_count": 16,
    "metadata": {},
    "outputs": [
     {
        "6"
       ]
      },
-     "execution_count": 33,
+     "execution_count": 16,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 31,
+   "execution_count": 21,
    "metadata": {},
    "outputs": [
     {
-     "data": {
-      "text/plain": [
-       "'the rebellion led by queen boudicca took strength from the defeat of the ninth legion the iceni were joined by the trinovantes tribe as they setup on both londinium and verulam ium they razed the cities to the ground and desecrated our temples suetonius informed by varus legatus of the ix legion of the seriousness of the situation returned from subduing rebels on the island of mona and confronted the enemy at viro conium a she travelled back along watling street in his own words despite being outnumbered ten to one the bravery and heroism of our forces and my careful choice of the battleground and tactics ensured our decisive and glorious victory whoever masterminded the battle the enemy lost many thousands against just a few hundred of our own troops and agricola was left to secure the region acting with ruthless efficiency perhaps in part to at one for his guilt over the previous loss he supressed the rebellion boudicca possessed of the perilous ness of her situation took her own life cursing the ixth legion as she died but a battle is not a war and the true peril was about to be revealed a captured enemy spy revealed that they had be enable to decipher captured battle orders and communications which had been encrypted using the caesar cipher this device invented by julius caesar himself had secured military and diplomatic communications across the empire for decades it was no longer safe and neither were we suetonius s despatches to the emperor were careful to give himself the credit for the defeat of boudicca and left no doubt that agricola was to blame for the uprising the loss of the legions aquila was said to have encouraged the local tribes to rebel and it was hinted that agricola may also have been to blame for the loss of the caesar cipher what suetonius failed to reveal was that it was agricola who engineered the defeat of the tribes on watling street but if you wish to know the truth of that day then you will need to travel further to the isle of thorns'"
-      ]
-     },
-     "execution_count": 31,
-     "metadata": {},
-     "output_type": "execute_result"
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "the rebellion led by queen boudicca took strength from the defeat of the ninth legion the iceni were\n",
+      "joined by the trinovantes tribe as they setup on both londinium and verulam ium they razed the\n",
+      "cities to the ground and desecrated our temples suetonius informed by varus legatus of the ix legion\n",
+      "of the seriousness of the situation returned from subduing rebels on the island of mona and\n",
+      "confronted the enemy at viro conium a she travelled back along watling street in his own words\n",
+      "despite being outnumbered ten to one the bravery and heroism of our forces and my careful choice of\n",
+      "the battleground and tactics ensured our decisive and glorious victory whoever masterminded the\n",
+      "battle the enemy lost many thousands against just a few hundred of our own troops and agricola was\n",
+      "left to secure the region acting with ruthless efficiency perhaps in part to at one for his guilt\n",
+      "over the previous loss he supressed the rebellion boudicca possessed of the perilous ness of her\n",
+      "situation took her own life cursing the ixth legion as she died but a battle is not a war and the\n",
+      "true peril was about to be revealed a captured enemy spy revealed that they had be enable to\n",
+      "decipher captured battle orders and communications which had been encrypted using the caesar cipher\n",
+      "this device invented by julius caesar himself had secured military and diplomatic communications\n",
+      "across the empire for decades it was no longer safe and neither were we suetonius s despatches to\n",
+      "the emperor were careful to give himself the credit for the defeat of boudicca and left no doubt\n",
+      "that agricola was to blame for the uprising the loss of the legions aquila was said to have\n",
+      "encouraged the local tribes to rebel and it was hinted that agricola may also have been to blame for\n",
+      "the loss of the caesar cipher what suetonius failed to reveal was that it was agricola who\n",
+      "engineered the defeat of the tribes on watling street but if you wish to know the truth of that day\n",
+      "then you will need to travel further to the isle of thorns\n"
+     ]
     }
    ],
    "source": [
-    "wcat(segment(column_transposition_decipher(sanitise(cb), \n",
+    "print(prettify(column_transposition_decipher(sanitise(cb), \n",
     "                                           tuple(range(274)), \n",
     "                                           fillcolumnwise=True, \n",
     "                                           emptycolumnwise=False)))"
   },
   {
    "cell_type": "code",
-   "execution_count": 34,
+   "execution_count": 22,
    "metadata": {},
    "outputs": [
     {
-     "data": {
-      "text/plain": [
-       "'the rebellion led by queen boudicca took strength from the defeat of the ninth legion the iceni were joined by the trinovantes tribe as they setup on both londinium and verulam ium they razed the cities to the ground and desecrated our temples suetonius informed by varus legatus of the ix legion of the seriousness of the situation returned from subduing rebels on the island of mona and confronted the enemy at viro conium a she travelled back along watling street in his own words despite being outnumbered ten to one the bravery and heroism of our forces and my careful choice of the battleground and tactics ensured our decisive and glorious victory whoever masterminded the battle the enemy lost many thousands against just a few hundred of our own troops and agricola was left to secure the region acting with ruthless efficiency perhaps in part to at one for his guilt over the previous loss he supressed the rebellion boudicca possessed of the perilous ness of her situation took her own life cursing the ixth legion as she died but a battle is not a war and the true peril was about to be revealed a captured enemy spy revealed that they had be enable to decipher captured battle orders and communications which had been encrypted using the caesar cipher this device invented by julius caesar himself had secured military and diplomatic communications across the empire for decades it was no longer safe and neither were we suetonius s despatches to the emperor were careful to give himself the credit for the defeat of boudicca and left no doubt that agricola was to blame for the uprising the loss of the legions aquila was said to have encouraged the local tribes to rebel and it was hinted that agricola may also have been to blame for the loss of the caesar cipher what suetonius failed to reveal was that it was agricola who engineered the defeat of the tribes on watling street but if you wish to know the truth of that day then you will need to travel further to the isle of thorns'"
-      ]
-     },
-     "execution_count": 34,
-     "metadata": {},
-     "output_type": "execute_result"
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "the rebellion led by queen boudicca took strength from the defeat of the ninth legion the iceni were\n",
+      "joined by the trinovantes tribe as they setup on both londinium and verulam ium they razed the\n",
+      "cities to the ground and desecrated our temples suetonius informed by varus legatus of the ix legion\n",
+      "of the seriousness of the situation returned from subduing rebels on the island of mona and\n",
+      "confronted the enemy at viro conium a she travelled back along watling street in his own words\n",
+      "despite being outnumbered ten to one the bravery and heroism of our forces and my careful choice of\n",
+      "the battleground and tactics ensured our decisive and glorious victory whoever masterminded the\n",
+      "battle the enemy lost many thousands against just a few hundred of our own troops and agricola was\n",
+      "left to secure the region acting with ruthless efficiency perhaps in part to at one for his guilt\n",
+      "over the previous loss he supressed the rebellion boudicca possessed of the perilous ness of her\n",
+      "situation took her own life cursing the ixth legion as she died but a battle is not a war and the\n",
+      "true peril was about to be revealed a captured enemy spy revealed that they had be enable to\n",
+      "decipher captured battle orders and communications which had been encrypted using the caesar cipher\n",
+      "this device invented by julius caesar himself had secured military and diplomatic communications\n",
+      "across the empire for decades it was no longer safe and neither were we suetonius s despatches to\n",
+      "the emperor were careful to give himself the credit for the defeat of boudicca and left no doubt\n",
+      "that agricola was to blame for the uprising the loss of the legions aquila was said to have\n",
+      "encouraged the local tribes to rebel and it was hinted that agricola may also have been to blame for\n",
+      "the loss of the caesar cipher what suetonius failed to reveal was that it was agricola who\n",
+      "engineered the defeat of the tribes on watling street but if you wish to know the truth of that day\n",
+      "then you will need to travel further to the isle of thorns\n"
+     ]
     }
    ],
    "source": [
-    "wcat(segment(column_transposition_decipher(sanitise(cb), \n",
+    "print(prettify(column_transposition_decipher(sanitise(cb), \n",
     "                                           tuple(range(6)), \n",
     "                                           fillcolumnwise=False, \n",
     "                                           emptycolumnwise=True)))"
   },
   {
    "cell_type": "code",
-   "execution_count": 32,
+   "execution_count": 24,
    "metadata": {},
    "outputs": [
     {
        "1999"
       ]
      },
-     "execution_count": 32,
+     "execution_count": 24,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "open('2b.plaintext', 'w').write(wcat(segment(column_transposition_decipher(sanitise(cb), \n",
+    "open('2b.plaintext', 'w').write(prettify(column_transposition_decipher(sanitise(cb), \n",
     "                                           tuple(range(274)), \n",
     "                                           fillcolumnwise=True, \n",
-    "                                           emptycolumnwise=False))))"
+    "                                           emptycolumnwise=False)))"
    ]
   },
   {
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.5.3"
+   "version": "3.6.3"
   }
  },
  "nbformat": 4,