Done challenge 3
[cipher-tools.git] / 2020-early / 2020-a-challenge3.ipynb
diff --git a/2020-early/2020-a-challenge3.ipynb b/2020-early/2020-a-challenge3.ipynb
new file mode 100644 (file)
index 0000000..0c65ac3
--- /dev/null
@@ -0,0 +1,212 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import os,sys,inspect\n",
+    "currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))\n",
+    "parentdir = os.path.dirname(currentdir)\n",
+    "sys.path.insert(0,parentdir) "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from cipher.caesar import *\n",
+    "from cipher.affine import *\n",
+    "from cipher.keyword_cipher import *\n",
+    "\n",
+    "from support.text_prettify import *\n",
+    "from support.utilities import *\n",
+    "from support.plot_frequency_histogram import *"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "challenge_number = 3\n",
+    "plaintext_a_filename = f'{challenge_number}a.plaintext'\n",
+    "plaintext_b_filename = f'{challenge_number}b.plaintext'\n",
+    "ciphertext_a_filename = f'{challenge_number}a.ciphertext'\n",
+    "ciphertext_b_filename = f'{challenge_number}b.ciphertext'"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 18,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "ca = open(ciphertext_a_filename).read()\n",
+    "cb = open(ciphertext_b_filename).read()\n",
+    "\n",
+    "sca = sanitise(ca)\n",
+    "pca = letters(ca)\n",
+    "pta = depunctuate(ca)\n",
+    "\n",
+    "scb = sanitise(cb)\n",
+    "pcb = letters(cb)\n",
+    "ptb = depunctuate(cb)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 20,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "('problem', <KeywordWrapAlphabet.from_largest: 3>)"
+      ]
+     },
+     "execution_count": 20,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "(kworda, kwrapa), score = keyword_break_mp(sca, fitness=Ptrigrams)\n",
+    "kworda, kwrapa"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "harry,\n",
+      "i don't think this is about alchemy. the energy and raw materials required to produce valuable elements are far too expensive to make this practical, and no-one knows how to do it. it is possible that die alchemisten are focussed on this, but if so they are no threat to anyone at the moment. that doesn't mean we have nothing to worry about. the patent filed by leo szilard was for a prototype nuclear reactor. szilard is an engineer and physicist, and saw cockroft's claim that the nuclear transmutation reaction could not yield energy \"on an engineering scale\" as a challenge to be beaten. transmutation occurs when an atom is hit by an energetic neutron, and his idea was to trigger a process using lighter elements that produce a lot of neutrons as well as energy when hit, so that the process is self-sustaining. so far, his attempts to use elements like beryllium and indium have been unsuccessful, but in the last year hahn and strassman succeeded where he failed, using uranium, and it is possible that this could lead to a practical energy generation process. if that is what die alchemisten are working on then we have a much bigger problem. nuclear processes yield a million times more energy for each gram of fuel than the current chemical reactions like burning coal, and if the nazis could harness that then their steel and aluminium production would massively increase giving them a huge advantage in war. we don't know how far they have got with this technology, but i think we should assume that they are ahead of us and try to find out how far.\n",
+      "\n"
+     ]
+    }
+   ],
+   "source": [
+    "pa = keyword_decipher(ca, kworda, kwrapa)\n",
+    "print(pa)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 24,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1571"
+      ]
+     },
+     "execution_count": 24,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "open(plaintext_a_filename, 'w').write(pa)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 25,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "('borealis', <KeywordWrapAlphabet.from_last: 2>)"
+      ]
+     },
+     "execution_count": 25,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "(kwordb, kwrapb), score = keyword_break_mp(scb, fitness=Ptrigrams)\n",
+    "kwordb, kwrapb"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 26,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "phil, i am heading to sweden to speak with lise meitner. she was part of the team who split the atom and together with frisch has worked out the details of how it works. she is a brilliant chemist as well as a physicist and i am hoping she can shed some light on how likely it is that that die alchemisten could develop a nuclear power generation system from the process.\n",
+      "on the way back, i plan to also visit some people i know in norway. a new power source will be of no use to the nazis without the raw materials for manufacturing, and a lot of the major bauxite smelters in europe are based in the country. even without a new source of power the german military planners will have their eye on supply lines and the norwegian aluminium companies must be one of their prime targets. i thought i would drop in and try to get a sense of how well defended these places are. there may already have been approaches from germany as part of their arms build-up and it would be good to know that too. if the worst happens and norway is invaded, then i want to have some agents already embedded in the national infrastructure and we need to think about how we would secure communications in an occupied country. if you or the agency have any contacts there then let me know. with a little luck i may even get to see the northern lights while i am there.\n",
+      "\n"
+     ]
+    }
+   ],
+   "source": [
+    "pb = keyword_decipher(cb, kwordb, kwrapb)\n",
+    "print(pb)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 27,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1347"
+      ]
+     },
+     "execution_count": 27,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "open(plaintext_b_filename, 'w').write(pb)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.7.4"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4
+}