Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q59\n", "Your task has been made easy, as the encryption key consists of three lower case characters. Using [q59_data.txt](../raw/q59_data.txt), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text." ] }, { "cell_type": "code", "execution_count": 1, "id": "bd7ddc2f-5964-49c3-a927-774399b0b0a9", "metadata": {}, "outputs": [], "source": [ "encrypted_text = open(\"q59_data.txt\").read().split(\",\")\n", "\n", "def distribute_text_into_lists(text, n_list):\n", " result = [[ ] for _ in range(n_list)]\n", " for i,char in enumerate(text):\n", " result[i%n_list].append(char)\n", " return result\n", "\n", "def combine_lists_into_text(text_lists):\n", " result = \"\"\n", " for i in range(len(text_lists[0])):\n", " for j in range(len(text_lists)):\n", " result += text_lists[j][i]\n", " return result\n", "\n", "def get_possible_key(distributed_encrypted_text):\n", " lowcase_char_range = range(ord('a'),ord('z')+1)\n", " best_fit_key = 0\n", " best_fit_score = 0\n", " for k in lowcase_char_range:\n", " score = sum( k ^ int(char) == ord(' ') for char in distributed_encrypted_text )\n", " if score > best_fit_score:\n", " best_fit_score = score\n", " best_fit_key = k\n", " return chr(best_fit_key), [chr(best_fit_key ^ int(char)) for char in distributed_encrypted_text]\n", " \n", "\n", "def decode_text(encrypted_text,key_length):\n", " distributed_text_list = distribute_text_into_lists(encrypted_text,key_length)\n", " key = \"\"\n", " decrypted_text_list = []\n", " result = {}\n", " for distributed_text in distributed_text_list:\n", " temp = get_possible_key(distributed_text)\n", " key += temp[0]\n", " decrypted_text_list.append(temp[1])\n", " result[\"key\"] = key\n", " result[\"text\"] = combine_lists_into_text(decrypted_text_list)\n", " result[\"ascii_sum\"] = sum(ord(char) for char in result[\"text\"])\n", " return result" ] }, { "cell_type": "code", "execution_count": 2, "id": "9417c19a-10d4-49e5-b609-e2819d21446f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 6.66 ms, sys: 0 ns, total: 6.66 ms\n", "Wall time: 6.62 ms\n" ] }, { "data": { "text/plain": [ "{'key': 'exp',\n", " 'text': 'An extract taken from the introduction of one of Euler\\'s most celebrated papers, \"De summis serierum reciprocarum\" [On the sums of series of reciprocals]: I have recently found, quite unexpectedly, an elegant expression for the entire sum of this series 1 + 1/4 + 1/9 + 1/16 + etc., which depends on the quadrature of the circle, so that if the true sum of this series is obtained, from it at once the quadrature of the circle follows. Namely, I have found that the sum of this series is a sixth part of the square of the perimeter of the circle whose diameter is 1; or by putting the sum of this series equal to s, it has the ratio sqrt(6) multiplied by s to 1 of the perimeter to the diameter. I will soon show that the sum of this series to be approximately 1.644934066842264364; and from multiplying this number by six, and then taking the square root, the number 3.141592653589793238 is indeed produced, which expresses the perimeter of a circle whose diameter is 1. Following again the same steps by which I had arrived at this sum, I have discovered that the sum of the series 1 + 1/16 + 1/81 + 1/256 + 1/625 + etc. also depends on the quadrature of the circle. Namely, the sum of this multiplied by 90 gives the biquadrate (fourth power) of the circumference of the perimeter of a circle whose diameter is 1. And by similar reasoning I have likewise been able to determine the sums of the subsequent series in which the exponents are even numbers.',\n", " 'ascii_sum': 129448}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "decode_text(encrypted_text,3)" ] }, { "cell_type": "code", "execution_count": null, "id": "ce4795f8-4397-4d5d-a2fb-5fb3a7eef28c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q58
Next Notebook:
Project Euler Q67
Loading